Run root script or command as root in startup application in linux mint

5,022

You can't put it in rc.local because that runs at boot time, not AFTER you log in.

First, carefully edit your /etc/sudoers file from within the /etc dir:

sudo visudo

And as the last entry in the section that starts with:

#Allow members of group sudo to execute any command

add the following:

%sudo    ALL=(ALL:ALL) NOPASSWD: /path/to/script

For ease of use, I would add your script to a path that's already specified as secure for sudo users, as specified at the top of the sudoers file. For mine, I created my script in my home dir, then ran

chmod +x /my/file

then went to /usr/bin and ran

sudo ln -s /path/to/my/script nameOfScriptForMySymbolicLink

then I went to my Startup Apps window (location will differ for your distro), clicked on 'Add' and gave it a descriptive name, then put

/usr/bin/nameOfScriptForMySymbolicLink

in the command line, closed out, rebooted and logged in and it works a charm.

Share:
5,022

Related videos on Youtube

answerSeeker
Author by

answerSeeker

I come to stackoverflow looking for answers. Occasionally, if I can help, I'll help.

Updated on September 18, 2022

Comments

  • answerSeeker
    answerSeeker almost 2 years

    I have a wireless adapter that used to work in Linux mint 16 (dwa 171) after installing drivers for it. Now it won't work the same way anymore since I upgraded to linux mint 17.

    I've found the only way to make it work properly is to run the command sudo service network-manager restart I already tried putting this command in rc.local, rc2.local and rc3.local with no real luck. I want this command to run automatically right after login in with startup application program. Please tell me how to.

    • Warwick
      Warwick almost 10 years
      You could add it to your $HOME/.bash_profile (assuming that you are using bash as your default shell. If you are using a different shell, then you could add it to its configuration file (run a man on the shell that you use to identify the correct file). If it is an application that you need to have start this, then if it has a start up script, then you could add it to the start section of that script.
    • answerSeeker
      answerSeeker almost 10 years
      Yes, I'm using bash as my default shell. But I don't really know how to add it to my bash. If you could answer it with a proper solution with the steps and it works I might choose your answer.
    • Warwick
      Warwick almost 10 years
      Probably better to add it in /etc/rc.local before the exit 0 line. Someone else posted a similar problem here
    • answerSeeker
      answerSeeker almost 10 years
      @Warwick Yes, I know I have to add it before the exit 0 line. I'm not that of much of a noob.
  • Ned64
    Ned64 over 7 years
    It should be noted that a script in a user's home directory which can be executed as root using sudo is a security risk. The user (or any program run by them) could change that script at will and execute any command as root. You may not care, but I thought I'd state it for the record. Better: Create that script in /usr/bin or /usr/local/bin or /usr/opt/bin and make it owned by root, rwxr-xr-x.
  • sebasth
    sebasth almost 7 years
    The question asks specifically how to run the task right after log-in. This runs at system boot up.
  • Bumba laga
    Bumba laga almost 7 years
    The result is pretty much the same, i assumed that he wants the manager to be restarted right after boot, as it would make no sense to wait until he has actually logged in.