Skip to content

Instantly share code, notes, and snippets.

@siberianbear
Forked from bluenex/how-to-run.md
Created March 24, 2023 01:28
Show Gist options
  • Save siberianbear/bab9049ee5ea0acb72fdb4aaa35cdae1 to your computer and use it in GitHub Desktop.
Save siberianbear/bab9049ee5ea0acb72fdb4aaa35cdae1 to your computer and use it in GitHub Desktop.
Guide to run script on terminal in the background with log.

Running in the background

This command can be applied to anything run on cli, for example bash script and python script.

Two ways to do

There are two ways to run process in the background, nohup and & (ampersand).

Steps

  1. Run using nohup or &
  2. Keep process id if it returns
  3. Check process id if it does not return with ps aux | grep <what to kill>
  4. If wanna stop process manually run kill <pid> for & or kill -9 <pid> for nohup

Difference

&

Ampersand runs script in the subshell so if you logout from current shell, the subshell would be terminated. Also this would not forward any output so we need to do it manually.

Example

// this does not collect any output, but still print output to stdout (on console).
// it just does not receive ctrl+c
python test.py &

// this writes output to specified file, does not write anything to console.
python test.py > log.txt &

// this writes error to specified file, but still print normal output to stdout.
python test.py 2> log.txt &

nohup

nohup run script in background and will not be killed when you logout from the current shell. The process will be killed when sending SIGHUP signal (kill -SIGHUP) or kill the PID directly. The output will be forwarded to nohup.out in the directory being run command.

Example

nohup python test.py &

Summary

  • If you are logging in via SSH and want to logout after running a script, use nohup.
  • If you are running the script on the same pc and want to take control of which file to log the output, append or overwrite, use ampersand (&).
  • You dont need to kill background task manually if the script contains no infinite loop.

Tips & Tricks

  • If you want to just check whether it's finished or not, you can grep specific phrase in the output file. For example if you put print("FINISH") after everything is done you can simply run:
grep FINISH nohup.out

If there is output, it is finished.

Ref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment