3

On my new Ubuntu system I tried to start a Bash script automatically on system start up.

I found a lot of posts and how-to's about it. so I tried to make it via Crontab:

  • run crontab -e
  • add @reboot /cronjobs/demo.sh >> /cronjobs/cronLogs/demo.output
  • set execution permission to script with sudo chmod +x /cronjobs/demo.sh
  • reboot system

The output was created, but the script will not execute. So I tried another solution with rc.local file:

  • run sudo vi /etc/rc.local
  • added /cronjobs/demo.sh || exit 1
  • reboot system

But my script doesn't run. So I read that for reboot the script must be in /etc/rc0.d/. So I tried this:

  • move the script with mv /cronjobs/demo.sh /etc/rc0.d/K99_demo.sh
  • check permissions (all seems to be ok)
  • reboot system

Same thing - script will not be executed.

So, what's my error? Why my script can't be executed? I can execute my script if I run ./demo.sh after i switched to the folder with cd /cronjobs . The script is a demo-file which simply creates a folder:

#!/bin/sh
echo demo output
mkdir /cronjobs/demofolder

Edited: Replaced paths and filenames; added full content of demo.sh file

2
  • Can you clarify what you mean by "the output was created, but the script will not execute" please? Oct 29, 2016 at 23:05
  • @steeldriver i mean, that the file log.output was created after system startup - with my echo i typed in script.sh to log something. But the script script.sh would not be executed (means that the demofolder wasn't created).
    – devKyrios
    Oct 30, 2016 at 16:36

1 Answer 1

0

The easiest solution is using sudo powers create a file like this 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 the use of a script file altogether and works for all users regardless of their home directory name, ie /home/steve, /home/mary, etc.

Edit - Add sleep 10

For whatever reason cron is working too fast, or kernel is working too slow when making directories. An extra line sleep 10 was necessary between the two mkdir lines.

You may not need 10 seconds in between the two make directory commands but 10 works on my system with an SSD.

Edit 2 - Make full directory path in one command

As per comments below a simpler method is to 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) tells mkdir to automatically create all directory parent levels if they don't exist.
  • -v (long version --verbose) tells mkdir to print the names of all directories created.
17
  • yes. the file and folder names are not this what i used on my system. it is only a example how i used it - that you know what i have done ;)
    – devKyrios
    Oct 30, 2016 at 16:27
  • If your question doesn't contain what you REALLY typed, how are we supposed to give a REAL answer? Oct 30, 2016 at 16:40
  • I thought that the real paths and file names are not important for my question. The path and file names are correctly - without typo. And i only used other path and scriptnames - the call of it is the same. But if you need the real paths, i can edit my post with real paths and filenames.
    – devKyrios
    Oct 30, 2016 at 16:45
  • I think it is best to include real commands and real error messages (if any) that are displayed after the real commands are entered. Oct 30, 2016 at 16:54
  • 1
    You can use mkdir -p /cronjobs/demofolder to make a directory and create all necessary directories to make the whole path exist. It also won't treat "already exists" as an error - it will complete with a success exit code if the entire path already exists.
    – doug65536
    Aug 29, 2017 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .