Run a bash script with sudo, nohup and in the background
This command will run script.sh under super user and you can disconnect and come back later:
sudo nohup bash script.sh &
It’s useful if you don’t have Screen or you want the process to close when it finishes without a screen session hanging around.
By default, the output will go to nohup.out. You can change it by redirecting it to a file:
sudo nohup bash script.sh > script.out &
nohup runs a command immune to hangups. It redirects input and output of the process. If you want to connect to the server again and see the output, use tail, as below:
tail -f script.out
The command will be printing output as your command produces it as if it runs in your terminal session. Ctrl-C will terminate tail but not the nohup command.
If you need to kill the command before it finishes, find the process id first with:
sudo ps -Af | grep “bash script.sh”
sudo kill <process id>
I hope it helps.
3 Comments »
Leave a Reply
-
Recent
- OpenGL hardware acceleration through remote X11 SSH connection
- GDB: How do I set current source file for list and break commands
- How To Create and Seed a Torrent (Ubuntu server, Transmission)
- GIT TF: Undo shallow pull and pull squashed changeset
- Lynx on Windows 7 and lynx_bookmarks.html file problem
- Old MacBook Overheating and Installation of Mac OS 10.4 on New Hard Drives
- Memory Alignment Of Structures and Classes in C++
- Align label and input vertically
- Google Test Framework and Visual Studio 2010
- Convex Hull
- Run a bash script with sudo, nohup and in the background
- Contact database with web interface – EVPO Members
-
Links
-
Archives
- March 2017 (1)
- May 2015 (1)
- January 2015 (1)
- November 2014 (1)
- October 2014 (1)
- March 2014 (1)
- January 2014 (1)
- June 2013 (1)
- May 2013 (2)
- February 2012 (2)
- October 2010 (1)
- February 2010 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS
Just learned something new with ‘nohup’…
Can you do something similar with ‘screen’?
Yes, it was new for me too. I spent a lot of time making it work before I found the right combination of commands. Regarding screen yes, you can. I use screen for irssi (http://irssi.org/) client to log messages. However, there is one problem. When the script is finished, the screen session will stay open. I don’t know if the screen session can close automatically when the process quits.
Thanks for the post. It really helped.