0

I want to run the sh.01 shell-script at start-up for a specific user and so far I've edited a crontab using crontab -e -u bristena in which I wrote @reboot /home/bristena/test1/sh.01 My question is when does the shell script sh.01 actually run? I've tried writing reboot in the terminal but after that, when I click the terminal again, sh.01 doesn't run. What do I have to do to see the shell-script running at start-up? Thank you so much

3
  • A @reboot job is run as soon as the cron daemon starts at system startup (this would likely be before you are logged in). What evidence do you have that the script is not run?
    – Kusalananda
    May 25 '20 at 10:47
  • @kusalananda I just assumed that if the contents of the shell-script do not appear on the terminal after I wrote reboot then it means that sh.01 doesn't run. For example if the content of the shell script is echo "Hello World" then after I've edited the crontab as specified above and wrote reboot the first thing I would see when I open the terminal would be 'Hello World`. Is that not the case?
    – helpmb
    May 25 '20 at 12:01
  • No. The output of cron jobs are mailed to the user of the job (assuming local email delivery is possible, otherwise you would never get the output). If you want to output to a file, then redirect the output of the job to a file. @reboot jobs are run as soon as the cron daemon starts, not just before it shuts down after a user issues the reboot command. Also, since this cron job is run even before you log in, and since cron jobs are run totally separate from terminals or whatever else you happen to be running, there is no way for you get that message in the terminal directly from the job.
    – Kusalananda
    May 25 '20 at 12:40
0

In order for the script to run at boot, you need to register it in the /etc/rc.local file before the line exit 0. If you do not have this file, create it and paste the following contents into it:

#!/bin/sh -e
#here your script.
/usr/sbin/mescripts
exit 0

There is a directory /etc/network/ with subdirectories if-down.d, if-pre-up.d, if-post-down.d, if-up.d. If you place the script in one of these subdirectories, it will be executed accordingly when you turn it off, before turning it on, after turning it off, or when you turn on the network.

Another way is to specify one of the following directives in the /etc/network/ interfaces file: up, pre-up, post-up, down, pre-down, post-down. For example, the line:

post-up /path/to/script.sh

After turning on the network, it will execute the script.sh script. More details can be found in man interfaces.

In any case, the script must have rights that allow execution, otherwise it will not be able to start!

3
  • Scripts run via rc.local are running as root. The objective seems to be to run it as a specific user. You could do that with su username -c '/path/to/script'
    – Kusalananda
    May 25 '20 at 10:58
  • @Kusalananda♦ You are right!
    – bogdyname
    May 25 '20 at 10:59
  • I'm afraid I don't see how the network hooks are relevant. The OP doesn't say anything about wanting to run a script depending on the state of a network interface. And network interface hooks also don't run under arbitrary user accounts.
    – TooTea
    May 25 '20 at 13:16

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.