Reputation: 8185
How can I run a shell script and immediately background it, however keep the ability to inspect its output any time by tailing /tmp/output.txt
.
It would be nice if I can foreground the process too later.
It would be really cool if you can also show me how to "send" the backgrounded process in to a GNU screen that may or may not have been initialized.
Upvotes: 114
Views: 242267
Reputation: 240
I tried nohup
but my process was getting stopped after a day or two , so i tried this simple bash script to rerun my command after it stops
run.sh
while true; do
[ -e stopme ] && break
python3 main.py # your command here
done
then do
nohup bash run.sh &
Upvotes: 4
Reputation: 6999
One easy to use approach that allows managing multiple processes and has a nice terminal UI is hapless utility.
Install with pip install hapless
(or python3 -m pip install hapless
) and just run
$ hap run my-command # e.g. hap run python my_long_running_script.py
$ hap status # check all the launched processes
$ hap logs 4 # output logs for you 4th background process
$ hap logs -f 2 # continuously stream logs for the 2nd process
See docs for more info.
Upvotes: 8
Reputation: 2813
Simply add an ampersand (&
) after the command.
If the program writes to standard out, it will still write to your console / terminal.
Simply use the fg
command. You can see a list of jobs in the background with jobs
.
For example:
If you have already started the process in the foreground, but you want to move it to the background, you can do the following:
bg
command to resume the process, but have it run in the background instead of the foreground.Upvotes: 200
Reputation: 482
Another way is using the nohup
command with &
at the end of the line.
Something like this
nohup whatevercommandyouwant whateverparameters &
This will run it in the background and send its output to a nohup.log file.
Upvotes: 34