This command can be applied to anything run on cli, for example bash script and python script.
There are two ways to run process in the background, nohup
and &
(ampersand).
- Run using
nohup
or&
- Keep process id if it returns
- Check process id if it does not return with
ps aux | grep <what to kill>
- If wanna stop process manually run
kill <pid>
for&
orkill -9 <pid>
fornohup
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.
// 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
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.
nohup python test.py &
- 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.
- 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 putprint("FINISH")
after everything is done you can simply run:
grep FINISH nohup.out
If there is output, it is finished.