S
S
swim3r2018-03-21 12:01:27
linux
swim3r, 2018-03-21 12:01:27

How to make conditions for cron task?

Good afternoon dear experts.
Help solve this problem. There is a script to automate the deployment of a virtual host. The script should run on it. But executing this script interrupts the cron task.
This type of task

*/10 * * * * root timeout -k 1m 10m 900 ansible-pull $OPTS

Is it possible to add some kind of condition to this task, like [ ps fax || grep "process" -eq 0 ] to check if the script is running, and if it's still running, don't run the cron job.
For example, what would happen like this:
*/10 * * * * root [ условие ] || timeout -k 1m 10m 900 ansible-pull $OPTS

PS. Apparently I asked the wrong question. Interested in exactly what condition to make. What I wrote does not work.
P.P.S. I'm inclined to think that it's easier to add some kind of test file to the end of the script, and if it exists, then run cron. Experienced people have said that the ps and grep option is not very reliable.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
ky0, 2018-03-21
@ky0

Can't these two operations be placed in one cron job?
In general, of course, you can check for the existence of a process. I would write a small script that creates a lock file before starting and deletes or renames it at the end.

A
Alexander Shelemetiev, 2018-03-21
@zoroda

Can.
I have this line in my crontab:
Checks if today is a working day, if yes, then starts the music as an alarm

S
Saboteur, 2018-03-21
@saboteur_kiev

*/10 * * * * root [ условие ] || timeout -k 1m 10m 900 ansible-pull $OPTS

Everything should work, the main thing is to set the condition correctly.
should not work in your case, since cron runs ps without a terminal, and as the root user - it's not a fact that fax processes will be displayed. You can simply put ps fax > $HOME/temp.txt in the crown and look at the result in the list of processes.
That is, you need to either ps -xa or, even better, check the PID file (the script should be able to do this).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question