Automatically re-start script as root
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.
Win an iPhone 6
Enter to Win
|Geek Hide-away in Guatemala - Stay for Free!||Nov 26, 2015|
|Microsoft and Linux: True Romance or Toxic Love?||Nov 25, 2015|
|Non-Linux FOSS: Install Windows? Yeah, Open Source Can Do That.||Nov 24, 2015|
|Cipher Security: How to harden TLS and SSH||Nov 23, 2015|
|Web Stores Held Hostage||Nov 19, 2015|
|diff -u: What's New in Kernel Development||Nov 17, 2015|
- Microsoft and Linux: True Romance or Toxic Love?
- Geek Hide-away in Guatemala - Stay for Free!
- Cipher Security: How to harden TLS and SSH
- Non-Linux FOSS: Install Windows? Yeah, Open Source Can Do That.
- Web Stores Held Hostage
- Firefox's New Feature for Tighter Security
- It's a Bird. It's Another Bird!
- PuppetLabs Introduces Application Orchestration
- diff -u: What's New in Kernel Development
- IBM LinuxONE Provides New Options for Linux Deployment