Question about alternating commands every minute in Cron

I've been editing my crontab with crontab -e -u, and this is the contents:

*/1 * * * * export DISPLAY=:0 && xset dpms force off
*/1 * * * * export DISPLAY=:0 && xset dpms force on

How do I make the on time holding for 1 minute, so the display is off for 1 minute then on 1 minute?

4

1 Answer

It sounds like you want crontab lines that execute a command at alternating minutes, like the first command at even minutes (0,2,4 etc.) and the second command at odd minutes (1,3,5 etc.).

This can be done in the following way:

0-59/2 * * * * export DISPLAY=:0 && xset dpms force off
1-59/2 * * * * export DISPLAY=:0 && xset dpms force on

Explanation:

The minute entry here makes use of 2 different elements - ranges and step values.

The range has the format ?-?, so we define two different ranges, starting 1 minute apart (0-59 and 1-59).

The /2 part is the step value. By using this, we tell cron to only execute at every other value in the range.

By using the full possible minute range (but different starting values), we ensure that the commands run at every alternating minute, but 1 minute apart.

Fun fact:

The first range could also be entered as 0-58 and it wouldn't make a difference (since odd values are skipped in this range).

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like