Commands in Backgroud

How to run linux commands in background.

nohup

1
2
$ nohup sh ./test.sh &
$ nohup test.sh &
1
$ nohup test.sh > test.log 2>&1 &
  • 0 - stdin (standard input)
  • 1 - stdout (standard output)
  • 2 - stderr (standard error)
1
$ 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

1
# setsid sh ./test.sh

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

trap

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

screen

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