server - Start script on system startup (Ubuntu 16.04.1) - Ask Ubuntu
on new ubuntu system tried start bash script automatically on system start up.
i found lot of posts , how-to's it. tried make via crontab:
- run
crontab -e
- add
@reboot /cronjobs/demo.sh >> /cronjobs/cronlogs/demo.output
- set execution permission script
sudo chmod +x /cronjobs/demo.sh
- reboot system
the output created, script not execute. tried solution rc.local
file:
- run
sudo vi /etc/rc.local
- added
/cronjobs/demo.sh || exit 1
- reboot system
but script doesn't run. read reboot script must in /etc/rc0.d/
. tried this:
- move script
mv /cronjobs/demo.sh /etc/rc0.d/k99_demo.sh
- check permissions (all seems ok)
- reboot system
same thing - script not executed.
so, what's error? why script can't executed? can execute script if run ./demo.sh
after switched folder cd /cronjobs
. script demo-file creates folder:
#!/bin/sh echo demo output mkdir /cronjobs/demofolder
edited: replaced paths , filenames; added full content of demo.sh
file
the easiest solution using sudo powers create file in /etc/cron.d/
:
shell=/bin/sh path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin @reboot root mkdir /cronjobs @reboot root sleep 10 @reboot root mkdir /cronjobs/demofolder
this avoids use of script file altogether , works users regardless of home directory name, ie /home/steve, /home/mary, etc.
edit - add sleep 10
for whatever reason cron working fast, or kernel working slow when making directories. line sleep 10
necessary between 2 mkdir
lines.
you may not need 10 seconds in between 2 make directory commands 10 works on system ssd.
edit 2 - make full directory path in 1 command
as per comments below simpler method use:
shell=/bin/sh path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin @reboot root mkdir -p -v /cronjobs/demofolder
-p
(long version--parents
) tellsmkdir
automatically create directory parent levels if don't exist.-v
(long version--verbose
) tellsmkdir
print names of directories created.
Comments
Post a Comment