On Ubuntu 15.04 I have this file: /usr/local/bin/myscript (it's a script I made).

If I run this command under my account, it will do what I need it to do as a root user: sudo /usr/local/bin/myscript

I now want to make /usr/local/bin/myscript run on machine startup, but as a root user (as if I was running the sudo command but without having to type any password). How is this done on Ubuntu 15.04?

  • Do you want GUI password prompt for running script on starting machine or not?
    – Pandya
    May 12 '15 at 13:09

And now, the systemd answer.

You're using Ubuntu version 15. You have systemd. /etc/rc.local is at best a backwards compatibility mechanism in systemd. And as shown by the mess in the AskUbuntu question hyperlinked below, using it can go horribly wrong. So make a proper systemd service unit.

You are creating a local, non-system non-package, service unit, so the unit file will be in /etc/systemd/system/ which is where that type of units go. Let us call it /etc/systemd/system/myscript.service. It contains:

Description=user2580's script



If your script forks "in order to dæmonize" then stop it from doing so. That's completely unnecessary.

  • Run systemctl preset myscript.service (as the superuser) to have it start automatically at bootstrap.
  • Run systemctl start myscript.service (as the superuser) to manually start it right now.
  • Run systemctl status myscript.service to see its status.

Note that this does not execute your script in a context where it will be able to talk to an X server. It could be run before an X server is even started up. But you don't mention any requirement for being an X client, nor for other complexities that bite novices like a HOME environment variable. And those are subjects for other questions, in any case. So I'll leave it at that.

Further reading

  • In case it needs to execute after other services (e.g., mounting a VM shared drive), one way is to set type=idle instead of type=simple. Apr 22 '16 at 23:40
  • I get Failed to preset unit: File caps2esc.service: No such file or directory when I try to run systemctl preset caps2esc.service. Am I missing something here? I'm on Ubuntu 17.04
    – kshenoy
    May 18 '17 at 15:14

You can simply add a line to call the script in /etc/rc.local. This file is the last of the init scripts to be run.

Just make sure that /etc/rc.local is executable and owned by root.


You can use gksudo:-

       gksu - GTK+ frontend for su and sudo

       gksu is a frontend to su and gksudo is a frontend to sudo.  Their primary purpose is to run graphical commands
       that need root without the need to run an X terminal emulator and using su directly.
  • Open Startup Applications (System Settings > Startup Applications or gnome-session-properties).
  • Add new Entry with following command (gksudo myscript or gksudo /usr/local/bin/myscript):-


Click Add and this script will run when you startup/login to desktop.

Note:- Make sure gksu is installed in order to work with gksudo. (you can install it by sudo apt-get install gksu)

Another way is to use Askpass program which works with sudo -A:-

-A, --askpass
                 Normally, if sudo requires a password, it will read it from the user's terminal.  If the -A
                 (askpass) option is specified, a (possibly graphical) helper program is executed to read the user's
                 password and output the password to the standard output.  If the SUDO_ASKPASS environment variable
                 is set, it specifies the path to the helper program.  Otherwise, if sudo.conf(5) contains a line
                 specifying the askpass program, that value will be used.  For example:

                     # Path to askpass helper program
                     Path askpass /usr/X11R6/bin/ssh-askpass

Example I use zenity as askpass helper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.