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
user1156544
  • 121
  • 1
  • 1
  • 7
  • Are you using the user's cron or the roots one ? – Félicien Feb 14 '18 at 13:29
  • Because if you have to run it as root, you need to edit crontab for the root user (sudo crontab -e) – Félicien Feb 14 '18 at 13:30
  • 1
    Jobs inserted via `crontab -e` (whether your user or the root user i.e. `sudo crontab -e`) should NOT include the user field after the time spec - that's only for system jobs (i.e. `/etc/crontab`) – steeldriver Feb 14 '18 at 13:34
  • I am using it as "user", which is how ideally the script should run. But in any case, it is strange the case with the echo, I am doing something wrong somwhere - unless cron @reboot only works woth root – user1156544 Feb 14 '18 at 13:35
  • Thanks @steeldriver, although this is not the error, because I have used it with no user field too – user1156544 Feb 14 '18 at 13:36
  • *"This script calls other scripts that need sudo permissions"* - this means you have to either run this as elevated user (root) or configure passwordless sudo. – Robert Riedl Feb 14 '18 at 14:07
  • See also this [excellent answer on why this is a bad idea](https://askubuntu.com/a/173930/783023) – Robert Riedl Feb 14 '18 at 14:09
  • You should write a proper init scritpt – Panther Feb 14 '18 at 15:23
  • @RobertRiedl So what is the solution? I don't want to run as root, but the script needs to call other scripts as other users. And even the simple "echo" is not working. Also, I think both your links point to the same place. – user1156544 Feb 14 '18 at 15:24

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
  • 58,040
  • 10
  • 106
  • 136
  • 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