I am trying to run a script in the background even after closing the terminal. I have searched and searched and tried nohup
and disown
but neither seem to be working. When I close a terminal window, I get the typical Closing this window will terminate the running processes: watch.
message. That ends up terminating my background process, even when using nohup
or disown
. What could be the problem?
My code is a simple two lines
cmd="nohup watch -n 1 sudo /etc/block.sh > /dev/null"
$cmd & # blocks automatically
It is located in .bash_profile
, because I want it to start up whenever I open a new terminal.
You can ignore the sudo; I've already found a way to execute a sudo command without entering the password.
I am using Mac OSX.
In the new screen session, execute the command or script you wish to put in the background. Press Ctrl + A on your keyboard, and then D . This will detach the screen, then you can close the terminal, logout of your SSH session, etc, and the screen will persist.
Use bg to Send Running Commands to the Background You can easily send these commands to the background by hitting the Ctrl + Z keys and then using the bg command. Ctrl + Z stops the running process, and bg takes it to the background.
This is already answered, but the Screen utility seems like it would be perfect for this.
man screen
To view the documentation for screen.
www.ss64.com/osx/screen.html
to view slightly more user friendly documentation.
Start screen with a name and a script to run:
screen -S GWatch Scripts/gw_watch.sh
This starts a screen session named 'GWatch' and executes gw_watch.sh.
When a screen session is started, one has the option of disconnecting from it. This will leave the screen active in the background. It will remain active even after the user logs out (permissions notwithstanding).
Here is an example:
Put the following into the file (I often use textwrangler and / or nano).
#!/bin/bash
count=0
while [ $count -lt $1 ] ; do
echo "Count: $count of $1. Pausing for five seconds."
sleep 5s
((count++))
done
Open two terminal windows.
screen -ls
. You should see a message about no sockets being found.screen -S ScreenCheck screencheck.sh 500
. screencheck.sh has to be executable.In the second terminal window, you should see:
Count: 0 of 500. Pausing for five seconds.
Count: 1 of 500. Pausing for five seconds.
Count: 2 of 500. Pausing for five seconds.
...
ctrl-a d
. That's control + a, release both, d key.[detached]
.screen -ls
.You should see something like:
FCH000: ~: screen -ls
There is a screen on:
1593.ScreenCheck (Detached)
1 Socket in /var/folders/pk/l6b5fhkj6mxfpfh8mtgmstg40000gn/T/.screen.
Reattach to the screen session using screen -R ScreenCheck
.
You should see something like:
Count: 226 of 500. Pausing for five seconds.
Count: 227 of 500. Pausing for five seconds.
Count: 228 of 500. Pausing for five seconds.
Count: 229 of 500. Pausing for five seconds.
...
To see if it is running after logout, log out and ssh to the computer from another computer. screen -ls
should show the same screen session as before.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With