A
A
astrotrain2018-03-13 17:04:20
linux
astrotrain, 2018-03-13 17:04:20

How to track the status of the script in the crone and restart it if necessary?

I have such an entry in the crown, but it constantly spawns scripts:

*/10 * * * * ps aux|grep my_script| grep -v grep || exec /home/my_script > /home/log.txt &";

How can I make it so that it is normally checked whether an instance of the script is running and, if necessary, run it?
pidof can't be used because machines often don't have it. The truth is pgrep. But I would like a solution through ps.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
doublench21, 2018-03-13
@doublench21

So, what's with the cron? You just specify the sh script to run in the crone. And in the script itself, already do the checks you want. You can write the pid of the running one to a file and look for such one among the running ones before starting. Or perhaps you can find it by name in the list of processes. In general, everything is in your hands.

3
3vi1_0n3, 2018-03-14
@3vi1_0n3

You do not have the fact that the result of the pipe will be equal to non-zero, since this will be the return code of the last command in the pipe.
Try something like this:

*/10 * * * * [ "$(ps ax | grep my_script | grep -v grep)" == "" ] &&  /home/my_script > /home/log.txt &";

A
Andrey Shatokhin, 2018-03-13
@Sovigod

man lckdo

P
pfg21, 2018-03-13
@pfg21

write a systemd service for my_script
then the state of the service can be viewed through simple console commands.
But it is more efficient to prescribe restarting the service in case of a crash.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question