3

I am making a program for the Raspberry Pi to use, as part of its installation, it need to add a task to crontab so that it will run on bootup.

I know if I type in crontab -e then add the line "@reboot file/to/program" to the bottom it will do it.

But how do I do it with just a command in the terminal for my installation script to use?

2

This isn't particularly specific to the Raspberry Pi, and a bit of googling reveals some very complete cron overviews. From your description, it sounds like you want to edit the system-wide crontab (e.g.sudo nano /etc/crontab) to add the @reboot clause.

1

I have a number of crons that I add via my install script. Works perfect.

sudo sh -c 'cat <<EOF > /var/spool/cron/crontabs/root
00 * * * * curl http://example.org/ping/`hostname`
* * * * * /usr/bin/php /var/www/crontab.php
EOF'
sudo /etc/init.d/cron restart

If you want to do a manual check whether those crons have been added correctly:

sudo crontab -e

shows that everything is added as it should be.

0

Yet another option- you can place a file in /etc/cron.d/. The advantage of this is you own the whole file, so it's easier to install it and not worry about breaking other entries.

Example:

cat /etc/cron.d/hello
*/5 * * * * pi /home/pi/hello.sh

Note entries in system-level places like this also specify the username.

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.