How to run program in background mode and run it after we logoff Linux system?

Nohup command in Linux

nohup command is used to execute long running programs in background mode. Even if we logoff the Linux server, the program will continue to run in background. You can check the results or log file when you login again.Since the program will be started running in background and It leave the command prompt to user to run other commands

Syntax of nohup command

  • nohup means execute the command even after logoff the Linux machine.
  • Bash – used bash program to run our program.
  • ‘>’ means redirection operator to write output of program to log file.
  • ‘2’ means standard error device.
  • ‘&1’ means standard output device that is log file in our case.
  • ‘2>&1’ means writing error messages in log file.
  • & means run the program in background mode.

Example for nohup command

The sample script is stored in the file script.sh. Here the counter variable is initialized to zero. Then the while loop is used to validate the counter variable. If the value of the counter variable is not equal to 10, it will print the counter value.

Then the counter is incremented by 1 and while loop repeat the same until the condition failed. Sleep command is used to delay for a fixed amount of time during the execution of the script.

Output

The script is running in background mode using nohup command as below and 52401 is the process id generated for our script.

Nonup command example in Linux
nohup command example in Linux

Recommended Articles