Home » BASH » Write a bash script to keep programs/scripts running after logoff

Write a bash script to keep programs/scripts running after logoff

Update on:
Oct 4, 2021

Leading up to getting our scripts to run as daemons, we need to know how to keep commands running after a user logs off (or better yet, have them started by the system itself (we will look at this in more detail later). When a user logs in, a session for that user is created, but when they log off—unless the system owns it, processes and scripts typically get killed or closed.

This recipe is about keeping your scripts and activities running in the background after you log off.

Prerequisites

Besides having a terminal open, we need to remember a few concepts:

  • When a user logs off, any apps or processes owned by the current user will exit (the shell will send a signal)
  • The shell is configurable to not send a shutdown signal to processes
  • Applications and scripts use stdin and stdout for the usual operations
  • Applications or scripts in the background can be referred to as jobs

One neat way is by using &, which is used this way: $ bash runforver.sh &. Unfortunately, using only this technique, we are back at square one—our binary still dies when we exit. Therefore, we need to use programs such as screendisown, and sighup.

Note:

The screen command is not available on all systems. It is recommended that we use another command in case screen is absent (it is still useful to know!).

Write Script:

Let’s start our activity as follows:

Open a terminal and create the loop_and_print.sh script:

[et_pb_dmb_code_snippet title=”loop_and_print.sh” code=”IyEvYmluL2Jhc2ggCkVYSVRfUExFQVNFPTAgCklOQz0wIAoKdW50aWwgWyAke0VYSVRfUExFQVNFfSAhPSAwIF0gIyBFWElUX1BMRUFTRSBpcyBzZXQgdG8gMCwgdW50aWwgd2lsbCBuZXZlciBiZSBzYXRpc2ZpZWQgCmRvIAoJZWNobyAiQm9vICRJTkMiID4gL2Rldi9udWxsIAoJSU5DPSQoKElOQyArIDEpKSAKCXNsZWVwIDEgCmRvbmUgCmV4aXQgMA==” _builder_version=”4.9.4″ _module_preset=”369a0a83-f997-4079-bcdc-749886c9e1c7″ hover_enabled=”0″ sticky_enabled=”0″]IyEvYmluL2Jhc2ggCkVYSVRfUExFQVNFPTAgCklOQz0wIAoKdW50aWwgWyAke0VYSVRfUExFQVNFfSAhPSAwIF0gIyBFWElUX1BMRUFTRSBpcyBzZXQgdG8gMCwgdW50aWwgd2lsbCBuZXZlciBiZSBzYXRpc2ZpZWQgCmRvIAoJZWNobyAiQm9vICRJTkMiID4gL2Rldi9udWxsIAoJSU5DPSQoKElOQyArIDEpKSAKCXNsZWVwIDEgCmRvbmUgCmV4aXQgMA==[/et_pb_dmb_code_snippet]

Open a terminal and run the following commands:

[et_pb_dmb_code_snippet code=”JCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgCiQgcHMgYXV4IHwgZ3JlcCBsb29wX2FuZF9wcmludC5zaCAjIFRha2Ugbm90ZSBvZiB0aGUgUElEIC0gd3JpdGUgaXQgZG93bg==” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgCiQgcHMgYXV4IHwgZ3JlcCBsb29wX2FuZF9wcmludC5zaCAjIFRha2Ugbm90ZSBvZiB0aGUgUElEIC0gd3JpdGUgaXQgZG93bg==[/et_pb_dmb_code_snippet]

Next, log off, then log in and run the following command in a new terminal:

[et_pb_dmb_code_snippet code=”JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoICMgVGFrZSBub3RlIG9mIHRoZSBQSUQgLSB3cml0ZSBpdCBkb3du” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoICMgVGFrZSBub3RlIG9mIHRoZSBQSUQgLSB3cml0ZSBpdCBkb3du[/et_pb_dmb_code_snippet]

Can you find the process running? Next, run the following command:

[et_pb_dmb_code_snippet code=”JCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgIyBub3RlIHRoZSBQSUQgYWdhaW5nIAokIGRpc293bg==” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgIyBub3RlIHRoZSBQSUQgYWdhaW5nIAokIGRpc293bg==[/et_pb_dmb_code_snippet]

Next, log off, then log in and run the following command in a new terminal:

[et_pb_dmb_code_snippet code=”JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoICMgVGFrZSBub3RlIG9mIHRoZSBQSUQgLSB3cml0ZSBpdCBkb3du” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoICMgVGFrZSBub3RlIG9mIHRoZSBQSUQgLSB3cml0ZSBpdCBkb3du[/et_pb_dmb_code_snippet]

Next, run the following command:

[et_pb_dmb_code_snippet code=”JCBub2h1cCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICY=” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBub2h1cCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICY=[/et_pb_dmb_code_snippet]

Next, log off, then log in and run the following command in a new terminal:

[et_pb_dmb_code_snippet code=”JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoICMgVGFrZSBub3RlIG9mIHRoZSBQSUQgLSB3cml0ZSBpdCBkb3du” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoICMgVGFrZSBub3RlIG9mIHRoZSBQSUQgLSB3cml0ZSBpdCBkb3du[/et_pb_dmb_code_snippet]

How script work:

In step 1, we opened a terminal and created the loop_and_print.sh script. This script merely loops forever, printing as it does.

The following commands will use the loop_and_print.sh script and run in the background as a job. The ps command outputs process information and is piped through grep to simplify the output. In the command, we can see the process ID (PID) next to the username column. Keep note of PIDs so that you can kill zombie processes or stop unnecessary applications:

[et_pb_dmb_code_snippet code=”JCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgClsxXTQ1MTAgCiQgcHMgYXV4IHwgZ3JlcCBsb29wX2FuZF9wcmludC5zaCAjIFRha2Ugbm90ZSBvZiB0aGUgUElEIC0gd3JpdGUgaXQgZG93biAKcmJyYXNoIDQ1MTAgMC4wIDAuMCAxMjU0OCAzMDI0IHB0cy8yIFMgMTI6NTggMDowMCBiYXNoIGxvb3BfYW5kX3ByaW50LnNo” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgClsxXTQ1MTAgCiQgcHMgYXV4IHwgZ3JlcCBsb29wX2FuZF9wcmludC5zaCAjIFRha2Ugbm90ZSBvZiB0aGUgUElEIC0gd3JpdGUgaXQgZG93biAKcmJyYXNoIDQ1MTAgMC4wIDAuMCAxMjU0OCAzMDI0IHB0cy8yIFMgMTI6NTggMDowMCBiYXNoIGxvb3BfYW5kX3ByaW50LnNo[/et_pb_dmb_code_snippet]

Logging back on and running the ps command will produce zero results. This is because the script we put into the background using & has been sent a signal to shutdown or die. 

Again, we run the loop_and_print.sh script; command puts it into the background, and disown removes the the background process(es) from the known list of jobs. This disconnects the script and all output from any terminal. 

Upon logging back in and using the ps command, you shall see the PID of the command:

[et_pb_dmb_code_snippet code=”cmJyYXNoIDgwOTcgMC4wIDAuMCAxMjU0OCAzMDI0IHB0cy8yIFMgMTM6MDIgMDowMCBiYXNoIGxvb3BfYW5kX3ByaW50LnNo” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]cmJyYXNoIDgwOTcgMC4wIDAuMCAxMjU0OCAzMDI0IHB0cy8yIFMgMTM6MDIgMDowMCBiYXNoIGxvb3BfYW5kX3ByaW50LnNo[/et_pb_dmb_code_snippet]

The nohup command is similar to the disown command, except that it explicitly disconnects the script from the current shell. It is also different from disown because nohup allows you still retain output from the script, which is accessible by other applications after the fact in the nohup.out file:

[et_pb_dmb_code_snippet code=”JCBub2h1cCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgClsyXSAxNDI1NiAKJCBub2h1cDogaWdub3JpbmcgaW5wdXQgYW5kIGFwcGVuZGluZyBvdXRwdXQgdG8gJ25vaHVwLm91dCc=” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBub2h1cCBiYXNoIGxvb3BfYW5kX3ByaW50LnNoICYgClsyXSAxNDI1NiAKJCBub2h1cDogaWdub3JpbmcgaW5wdXQgYW5kIGFwcGVuZGluZyBvdXRwdXQgdG8gJ25vaHVwLm91dCc=[/et_pb_dmb_code_snippet]

Upon logging back in and using the ps command, you shall see the PIDs of the two scripts that survived the logoff:

[et_pb_dmb_code_snippet code=”JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoIApyYnJhc2ggNDUxMCAwLjAgMC4wIDEyNTQ4IDMwMjQgcHRzLzIgUyAxMjo1OCAwOjAwIGJhc2ggbG9vcF9hbmRfcHJpbnQuc2ggCnJicmFzaCAxNDI1NiAwLjAgMC4wIDEyNTQ4IDMwMjQgcHRzLzIgUyAxMzowMiAwOjAwIGJhc2ggbG9vcF9hbmRfcHJpbnQuc2g=” _builder_version=”4.9.4″ _module_preset=”3a2d4e4b-f2ae-4571-a284-ca584312491f” hover_enabled=”0″ sticky_enabled=”0″]JCBwcyBhdXggfCBncmVwIGxvb3BfYW5kX3ByaW50LnNoIApyYnJhc2ggNDUxMCAwLjAgMC4wIDEyNTQ4IDMwMjQgcHRzLzIgUyAxMjo1OCAwOjAwIGJhc2ggbG9vcF9hbmRfcHJpbnQuc2ggCnJicmFzaCAxNDI1NiAwLjAgMC4wIDEyNTQ4IDMwMjQgcHRzLzIgUyAxMzowMiAwOjAwIGJhc2ggbG9vcF9hbmRfcHJpbnQuc2g=[/et_pb_dmb_code_snippet]

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

Your email address will not be published.

3 + 1 =