3

How would I write a bash script that checks there are no currently running cron jobs, then does some simple action?

I am not talking about cron jobs that are scheduled to run at some point, I am referring to actively running processes.

Thanks!

OneSolitaryNoob
  • 5,423
  • 3
  • 25
  • 43
  • This is a very interesting question. I don't have an answer, but a suggestion: can you run through the `ps` list, and look to see if the parent is cron pid, or does cron clean the parent of administratively started processes? – PaulProgrammer Jul 17 '13 at 21:42

2 Answers2

4

intresting question ;)

for pid in `pgrep cron`;do
  ps uh --ppid $pid;
done|grep -v CRON
Zoltán Haindrich
  • 1,788
  • 11
  • 20
  • Thanks, how does this work? I am not sure of the structure of cron processes -- actually looks like it doesn't quite work, looks like CROND might start the processes? – OneSolitaryNoob Jul 17 '13 at 22:52
  • all jobs which are run by cron are started from a forked process created by cron. the above iterates thru all processes which have forked by cron (pgrep cron), and for all these pids lists these processes childrens – Zoltán Haindrich Jul 19 '13 at 07:32
0

You can read the crontab like this

crontab -l | grep -v "^#" | awk '{print $6}'

and check if any of the script/job is running

deagh
  • 2,647
  • 1
  • 30
  • 29