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.
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?
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:
pgrep
command returns the PID for crond where the parent PID is 1.pstree
command returns the process tree for the crond process.sed
command splits the process tree output into multiple lines. (For purposes of the next command.)grep
is excluding crond itself from the output.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
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.
this is also very powerful for text processing
printf "%s\n%s\n%s\n" "$OUTPUT1" "$OUTPUT2" "$OUTPUT3" | cat - /var/startup/forex/unhandled/num.txt >> temp && mv temp /var/startup/forex/unhandled/num.txt
prints line by line in a textfile. you need to define output tho