14

I need to start a script and a binary file from /usr/bin at startup with sudo/root privileges. I've tried many guides from this forum but nothing worked (rc.local and systemd style, Upstart) - maybe I'm doing something wrong. My OS is Ubuntu GNOME 16.10.

The script would be something like this

#!/bin/bash
sudo ./usr/bin/myprogram
sudo bash /home/USER/script.bash

Both the script and the program are executable. I just don't know the optimal way to start them with sudo at startup.

Please be gentle. I'm a new user.

  • 1
    Do those apps require GUI ? Are you trying to run standalone items or you want them to appear in GUI when you sign in ? You can run all that from rc.local (as explained by anonymous2 below) and you don't need sudo if it's command-line app; all because rc.local runs as root already, but it won't work for GUI apps. – Sergiy Kolodyazhnyy Mar 05 '17 at 00:01
  • One is a simpe script for fan control and the other is a VPN client with a GUI. The script just needs to execute and write to a file, but the VPN client should start and run until shutdown. – Benjamin Hunter Mar 05 '17 at 00:02
  • 1
    Ok you will probably want to split them. Fan control script can be by itself in rc.local. VPN client is trickier. Add that to startup applications , prefixing command with pkexec. I am on mobile right now so cannot post full answer, but will do so as soon as i get home. – Sergiy Kolodyazhnyy Mar 05 '17 at 00:14

3 Answers3

16

For command-line applications that do not require GUI, it is sufficient to place the call to them into /etc/rc.local , which already runs as root, thus does not need sudo. Below is example of my own /etc/rc.local which I use for starting two monitoring scripts.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/home/xieerqi/bin/batmon.sh &
/home/xieerqi/bin/sh/temperature.sh  &
 exit 0

For GUI applications, you would need to take a different approach. You would need to open Startup Applications app, and add the following command:

bash -c "sleep 10;gksu /usr/bin/my_vpn_program"

What this does is gives GUI sufficient time to start, 10 seconds, then will bring up password dialog and if you enter password properly, will launch your command. Effectively this is a mini bash script. You can use pkexec instead, and some might even say pkexec is recommended instead of gksu.

Alternatively, if you don't want to enter password every time, you can allow your user run this particular command with root privileges without authentication. For that you need to edit /etc/sudoers file. WARNING: it is recommended that you use sudo visudo to edit the file from terminal. Below is example of how I use the same setting with pm-suspend command:

# Allow using pm-suspend for my user without password
my_username_here ALL = NOPASSWD: /usr/sbin/pm-suspend

This line should be appended to the end of /etc/sudoers file and saved. Note, that you still need to append sudo or gksu to the beginning of each command that you set up. Thus, you would need to use the same bash command I showed previously.

rubo77
  • 32,486
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
2

Use a crontab option to make your script run after reboot,

you can do it by adding @reboot code in cron

Open crontab by root user:

$ sudo crontab -e

Add the next record at the bottom:

@reboot /usr/bin/myprogram

That will do what you want.

rubo77
  • 32,486
2

The solution is so simple you missed it. :) Because rc.local is run by root, the sudo's in your file are totally unnecessary.

In other words, put it in rc.local as you suggested, but omit the sudo's from your script. They are totally unnecessary, since rc.local is already run as root.

Hope this helps!

anonymous2
  • 4,298
  • 1
    I've read somewhere that rc.local service is disabled on Ubuntu 16.10, though, there is a guide on how to re-enable it. Is it safe to enable rc.local if it is disabled by default in terms of security or any other con? – Benjamin Hunter Mar 05 '17 at 00:04