Is it possible to launch a command or Bash script exit terminal and NOT interrupt command?
My solution was to run cron at a specific time of day, but I'm sure there is something easier.
To avoid exit signals propagating to child processes of the terminal and shell, run the command with nohup
, i.e.:
nohup cmd &
To ignore all program output and avoid the nohup.out
file, you can redirect stdout
and stderr
to /dev/null
like this (with bash):
nohup cmd &> /dev/null &
cmd & disown
works too, since the &
is treated like a ;
command separator. The disown command removes the connection between the bash shell session and the backgrounded command.
nohup
creates a file when it runs, for me, so I added > /dev/null
to ignore the output.
If you want to run a specific command or file every second or so in the background after exiting the terminal you could try this easy little thing;
nohup watch -n5 'bash script.sh' &
That would run scipt.sh every 5 seconds.
Using screen
:
screen -S <session name> -d -m <your command>
after that you can quit the terminal, also you can reattach to it by:
screen -r <session name>
More info: reference
Put a "&" character after your command.
e.g:
/home/your/script.sh &
/home/your/script.sh
-- as it was not detached from its "parent", but just "backgrounded". Use nohup
to detach it for real.
nohup
-- which also logs all (now invisible) output into a file called nohup.out
located in the directory you started the command from.
jobs
command. Quite often i want to be able to get back to the shell process after backgrounding it.
Dec 7, 2018 at 19:17