I have some shell scripts that will perform certain actions only on a weekend (Saturday or Sunday). There are two ways to solve the “is it a weekend” logic.
The script can be scheduled via a cron job and under the cron entry, the “day of week” field must be specified as “0,6
” (0 is Sunday, 6 is Saturday). On Linux, both 0 and 7 indicate Sunday.
1 |
0 8 * * 0,6 /path/to/weekend-script" |
The above crontab entry will run the script every weekend at 8am.
The other method is to add some logic in the script to determine if the current day is a weekend.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/bin/sh # get day of week day=`date +%u` # reset weekend indicator flag weekend=0 # if day of week is 6 or 7, then it's a weekend [ $day -ge 6 ] && weekend=1 # exit if not weekend [ $weekend -ne 1 ] && exit 0 # enter commands to be executed on weekends here # ... |