0

Is there a way to see a list of running cron jobs on my server?

I would like to see the name of the file being loaded or command. Plus how long it as been running.

I am on CentOS.

2

The cron daemon just runs commands on behalf of users. You can look in /var/log/cron to find out what commands have been run and you can look in all the usual places (/var/spool/cron/, /etc/crontab, /etc/cron.d/, /etc/cron.*, /etc/anacrontab &c ) to get a list of the commands that cron runs.

Beyond that the running time of each process will vary and be quite hard to track down, you're almost certainly not up to the job. What do you actually want to achieve?

2

This is quick and dirty. I almost feel embarrassed to post something with so many pipes as I am sure there are more efficient ways. However, it gets the job done and you can learn from it and improve upon it yourself.

You can parse the process tree for crond to find the current child processes.

cronpid="$(pgrep -P 1 crond)"
cronjobs="$(pstree -p $cronpid | sed 's/)/)\n/g' | grep -v crond | grep -wo '[0-9]*')"

Explanation:

  • The pgrep command returns the PID for crond where the parent PID is 1.
  • The pstree command returns the process tree for the crond process.
  • The sed command splits the process tree output into multiple lines. (For purposes of the next command.)
  • The first grep is excluding crond itself from the output.
  • The second grep is extracting the PIDs for the remaining processes. The output has now been reduced to just the running cron jobs. ($cronjobs)

Now, you can loop against the PIDs returned above and make a separate query for each job to determine how long it has been running and what the command was.

for job in ${cronjobs[@]}; do
  ps -p "$job" -o etime,cmd --no-headers
done

Example output:

      01:06 /usr/bin/sleep 90
      00:06 /usr/bin/sleep 90
3
  • Won't this only capture cron jobs that are present on the system when it is run ?
    – user9517
    Jul 31 '15 at 18:13
  • Unless I misunderstood completely, this is what he asked for. Jul 31 '15 at 19:57
  • Yeah it's not really clear what the OP wants really. I kinda thought they wanted to know the total of cron jobs that had run, not just those present on the system at any given moment.
    – user9517
    Jul 31 '15 at 20:41
0

Also, crontab has the -u option to list the cron jobs for a particular user. For run time, if the job has output, it will be emailed to the user, so you could get the difference between the scheduled start time and the arrival of the email for a rough idea of how long a job takes. cron itself does not track run time.

For currently running jobs, difficult to get without reference to the cron schedule, unless the jobs are run from /etc/cron.*. In that case, ps can be used to track such jobs.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.