Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

2 votes
696 views
696 views

I have the following cron job:

 /1 * * * * root /usr/local/bin/bash /usr/local/bin/vpn_monitor.sh 1> /usr/local/bin/log.txt 2> /usr/local/bin/err.txt

My issue is that it never runs via cron but it does when I run the command in standalone mode in shell. My log.txt and err.txt remain empty.

I added SHELL and PATH on top of my script but this doesn't work neither (via cron)

#!/bin/bash
SHELL=/bin/sh
PATH=/usr/local/bin:/usr/local/bin:/usr/bin

my script checks a ping and then runs a php file also in /usr/local/bin

I tried changing my cron job command to:

 /1 * * * * root sh /usr/local/bin/vpn_monitor.sh
 /1 * * * * root bash /usr/local/bin/vpn_monitor.sh

neither work (but they do in shell standalone.

What am I doing wrong?

in Scripting
by
edit history

Please log in or register to answer this question.

1 Answer

0 votes

Your timespec is incorrect. The time specification of a crontab entry consists of 5 fields:

  • minute (allowed values: 0-59)
  • hour (allowed values: 0-23)
  • day of the month (allowed values: 1-31)
  • month (allowed values: 1-12)
  • weekday (allowed values: 0-7)

The values can be specified as single values (e.g. 4), or ranges of values (e.g. 2-5). Multiple values and/or ranges can be specified as comma-separated lists (e.g. 2,4-8,13). An asterisk (*) is a shortcut for the full range. Months and weekdays can also be specified by name instead of by number.

By adding /{step} to a field you can further discriminate the number of executions for the given time field. /2 means run every second time, /5 means run every 5th time, etc. However, you cannot use that discriminator by itself. It must have a time frame to refer to.

If we use the "minute" field as an example, you can specify things like this:

  • */5 → run every 5 minutes (equivalent to 0,5,10,15,20,25,30,35,40,45,50,55)
  • 0-10/2 → for the first 10 minutes of the hour run every other minute (equivalent to 0,2,4,6,10)

Using /1 (step width of 1, i.e. run every time) doesn't make sense, though, as that's already the default. To have your script run every minute you'd simply put an asterisk in the "minute" field:

* * * * * root sh /usr/local/bin/vpn_monitor.sh
* * * * * root bash /usr/local/bin/vpn_monitor.sh

For further information see man 5 crontab.


As a side-note, setting $SHELL inside your scripts isn't all that useful. Just specify the desired interpreter in the shebang: #!/bin/sh for Bourne shell, #!/bin/bash for Bash. If your shell binaries are not in /bin, adjust the path accordingly, e.g. #!/usr/local/bin/bash.

by (115)
2 18 33
edit history
 
thank you. it worked with */5
...