Loading
Home ›
Automatically re-start script as root
Apr 17, 2008 By Mitch Frazier
in
If you use sudo you can automatically re-start scripts that need to be run as root by adding a check at the beginning of the script and executing sudo if the script is not running as root:
if [[ $UID -ne 0 ]]; then
sudo -p 'Restarting as root, password: ' bash $0 "$@"
exit $?
fi
The inclusion of "bash" in the sudo command is to avoid problems if the script does not have its execute bit set. The "exit $?" causes the shell to exit with the status from the script instance that sudo runs.
______________________
Mitch Frazier is an Associate Editor for Linux Journal.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android
- Employment Posters
3 hours 40 min ago - Sure the best distro is
5 hours 28 sec ago - BeOS was the best
7 hours 44 min ago - I use Wireshark on a daily
12 hours 14 min ago - buena información
17 hours 21 min ago - One important "bucket" that I didn't note (désolé si qqun deja d
18 hours 21 min ago - Gnome3 is such a POS. No one
1 day 3 hours ago - Gnome 3 is the biggest POS
1 day 3 hours ago - I didn't knew this thing by
1 day 10 hours ago - Author's reply
1 day 13 hours ago





Comments
Thanks Mitch!
Ever have one of those moments where you read something and it acts as a catalyst to remind you of something else?
This article reminded me that I had a service, that no matter what, would run as root, and I wanted it to run as the "nobody" user. That service is MRTG (and it's actually an older version from 2003 I've been using for a while). It makes me nervous because the daemon will just stop after a while.
It comes out it wasn't parsing the command line options correctly (I had -user=nobody instead of --user=nobody) and I was putting the process in the background with the `&' symbol instead of the --daemon option.
My mrtg is now correctly, (after all these years), running as "nobody" again.
My experience is almost irrelevant because of the fact that, as a perl script there are conventions within it to force the daemon to be ran as a particular user, in the script itself.
-- FLR or flrichar is a superfan of Linux Journal, and goofs around in the LJ IRC Channel