LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cron job (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-865267/)

fernfrancis 02-27-2011 01:33 AM

cron job
 
Hi

i am using centos 5.5
i have a couple of cron jobs running, some of them take about 20min to execute, i need to check the status of this jobs running. if they are currently being executed or they finish executing.

i went on the net there is a command "jobs". can this be used ,how to use it.

please help

EricTRA 02-27-2011 01:49 AM

Hello,

Also found on the net:
Quote:

jobs -x command [ args ... ]
The first form lists the active jobs. The options have the following meanings:
-l
List process IDs in addition to the normal information.
-p
List only the process ID of the job's process group leader.
-n
Display information only about jobs that have changed status since the user was last notified of their status.
-r
Restrict output to running jobs.
-s
Restrict output to stopped jobs.
If jobspec is given, output is restricted to information about that job. The return status is 0 unless an invalid option is encountered or an invalid jobspec is supplied.

If the -x option is supplied, jobs replaces any jobspec found in command or args with the corresponding process group ID, and executes command passing it args, returning its exit status.
I never used it so cannot indicate what it does or how it exactly works. I think since it's a job launched by cron you'd better check using pidof:
Code:

pidof name_of_your_command
to see if process IDs are still used by your script/command. On the other hand you could include a line in your script that writes something to a file in /tmp when done (for example date and time). Then by checking that file you'll know if your job is completed or not.

Kind regards,

Eric

unSpawn 02-27-2011 03:56 AM

The cron daemon already logs starting jobs in /var/log/cron. So you could include a line "logger -p cron.info -t "Starting jobname $$";" near the top of the cronjob and "logger -p cron.info -t "Exiting jobname $$";" near the end. The "$$" is the PID of the cronjob shell script, so any processes it starts should have their $PPID value set to this which makes it easier to search for using 'pgrep -lP $PPID'. Another reason to favor 'pgrep' over 'pidof' is that pgrep can use wildcards so running for instance 'pgrep -l cro*' will list any process that has the string "cro" in its process name. Finally, if you don't want to or can't modify your cronjob(s) but you can edit /usr/bin/run-parts and your cronjob or jobs are part of one of the default /etc/cron.{hourly,daily,weekly,monthly} directories /etc/crontab runs you could edit /usr/bin/run-parts to include logging jobs (in kinda-like-diff style):
Code:

        if [ -x $i ]; then
+              case "${i//*\//}" in
+                      *backup*|*audit*) logger -p cron.info -t "Starting job "${i//*\//}"";;
+              esac

The only drawback is that you need to track changes as /usr/bin/run-parts will be overwritten on upgrade of the crontabs package.

EricTRA 02-27-2011 08:12 AM

Hi,

Thanks for pointing out pgrep unSpawn, yet learned something new :-)

Kind regards,

Eric


All times are GMT -5. The time now is 11:39 PM.