简体   繁体   中英

How to run Bash Script in Crontab on Raspbian?

I have gone through all sorts of answers and replies up and down the Internet and nothing to seem to work for me. I want to simply run a bash script every minute using CRONTAB on Raspberry PI on Raspbian.

I have a script called autocon.sh and I simply entered into crontab as follows:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
...
1 * * * * root bash /home/pi/autocon.sh

BUT IT WON'T RUN. What am I doing wrong?

I'm not sure where the 'root' part comes from, but I'm guessing you're wanting to run the script as root? If so, you need to put an entry into the root crontab, do so by running:

sudo crontab -e

This will open up the root user crontab for editing, anything run from said location will run with root priveleges.

Insert the following line:

* * * * * bash /home/pi/autocon.sh

That should do it :) The 1 in your script actually means 'run at 1 minute past the hour' and thus in your case, 1 minute past every hour - easy mistake! Replacing it with a * means every minute.

The syntax is:

minute - hour - day of month - month - day of week - command 

Additionally, if you make your script executable, like so:

sudo chmod +x /home/pi/autocon.sh

You can omit the 'bash' command, and simply use:

* * * * * /home/pi/autocon.sh

And unless you're using the two lines at the top for something in particular, you can omit those too.

For clarity, Barmar's comment on my original post:

In per-user crontab files you don't put the username. But in /etc/crontab you do.

To run a cronjob every minute, all the values must be stars. Your cronjob is set to run at 1 minute past the hour every hour.

It should be:

* * * * * root bash /home/pi/autocon.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM