1

I am using Ubuntu Server 16.04. Everytime I log in, I need to manually run my script: /home/user/ini.sh. This script calls other scripts that need sudo permissions, so I get the prompt, insert my password and done. Now I want to configure a way to run the script automatically when the system starts.

I have used crontab -eand added the line

@reboot /home/user/ini.sh

This does not seem to work. I have tried different options as suggested by other users in here, here, here or here.

@reboot user    /home/user/ini.sh
@reboot root    /home/user/ini.sh
@reboot sh      /home/user/ini.sh
*/1 * * * *     /home/user/ini.sh
....

adding SHELL=/bin/bash... But I cannot get it to work. I have also tried

@reboot echo "yes" > /home/user/yes.txt

And the file is created - empty though, with no content inside (this demonstrates something is working but I am making a mistake somewhere).

Where is my error?


EDIT

I have also unsuccessfully tried to sudo crontab -e and use

@reboot root    /home/user/ini.sh

1 Answers1

4

There is a big difference between reboot and login, and your system treats them both quite differently.

  • REBOOT jobs are run by the root user (not your user), and must be headless (no display). The best way to do boot-time jobs in 16.04 and newer is to create a service, and include that service in the appropriate systemd target during the boot process.

  • LOGIN jobs are run by your user (not root) after you enter your password. The best way to do login jobs is to place them in your ~/.config/autostart/ directory.

But you have a second problem. You seem to want a user-level job to use root-level services. There are many, many ways to do this.

  • The easiest way is to simply run your script as root manually every day. You already know how to do this, so let's look at other options.

  • The fastest way is to hack at the sudoers file and generate a special permission set. However, it may not backup properly with the rest of your system, AppArmor may block some actions anyway, and it's hard to troubleshoot and maintain.

  • The appropriate way is to separate your user-level and root-level functions into two separate scripts, and to use dbus to launch the root-level script and return it's output. This is easy to maintain and troubleshoot, but requires the greatest skill and a bit of learning about dbus.

user535733
  • 62,253
  • That was insightful. I found another option for LOGIN jobs, which is the use of .profile (it needs the user to log in). I will do some testing and post the result – user1156544 Feb 14 '18 at 16:49