Commands in Backgroud

How to run linux commands in background.

nohup

$ nohup sh ./test.sh &
$ nohup test.sh &
$ nohup test.sh > test.log 2>&1 &
  • 0 - stdin (standard input)
  • 1 - stdout (standard output)
  • 2 - stderr (standard error)
$ nohup python -u test.py > test.log 2>&1 &

Sometimes I find that nohup.out does not show what is printed in the python program. This is because python’s output is buffered, so nohup.out doesn’t see the output right away. python has a -u parameter that makes python not enable buffering. Change it to the following command and you’re good to go!

setid

# setsid sh ./test.sh

This method makes the running process run as root, which is somewhat of a security risk.

trap

#!/bin/bash
trap "" HUP
while true;do
    date >> /root/test.txt
    sleep 1
done

screen

$ screen sh test.sh
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus