Saturday, January 8, 2022

[SOLVED] Bash script not executing on startup

Issue

I used crontab to run a bash script once a day (at midnight). This worked successfully, but I didn't really think it through enough to realise that the script would not run if my computer happened to be off at midnight (or even asleep).

So I think that I have to keep the crontab entry (in case my computer is on) but I also want to make the bash script execute on startup (in case my computer was off) so that I have covered all my bases.

I saw online that to have my script execute on startup I should edit my /etc/rc.local file. My file now contains:

#! /bin/bash                                                                    
bash /home/me/code.sh
exit 0

and my permissions are -rwxr-xr-x.

However, the script does not execute on startup. Is there something I'm doing wrong?


Solution

I guess you created the /etc/rc.local yourself right? AFAIK there isn't any in Ubuntu 18.04 anymore (as it is depricated). That's probably why it's not executed.

@reboot in crontab might be a possible solution for your problem.



Answered By - RadaD