can we check process id or process of scheduled cronjob script
let say, I have scheduled one script "abc.sh" which runs all time and I have scheduled it crontab like below -
* * * * 0-5 script.sh
Once I schedule, I know it will keep running but can I check process status whether "script.sh" is running or not
Use pgrep or pidof:
pgrep script.sh
Related
I'm currently running jobs on a computing cluster that uses the slurm workload manager. I can view all of the jobs I currently have running with:
$ squeue -u <username>
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
3379570 sixhour job01 <username> PD 0:00 1 (Priority)
3409269 sixhour job02 <username> R 03:06:13 1 n387
So in this example, I have two jobs that have been submitted. job01 is pending, and job02 has been running for around 3 hours.
I want to set up a cron job that will only submit the job if it's not listed in the above view. Take the following cron file as an example:
MAILTO=""
* */1 * * * sbatch job01.sh
* */1 * * * sbatch job02.sh
* */1 * * * sbatch job03.sh
Using this cron file, each job would get submitted every hour. But because job01 and job02 are already listed under squeue -u <username>, I only want job03 to actually get submitted.
Is there a way I can add some conditional logic to the cron file?
You can try a script like this:
#!/bin/bash
jobnames=$(squeue -h --user <username> --format %j)
for jobname in job{01..03} ; do
grep "$jobname" <<< "$jobnames" >/dev/null || sbatch "$jobname.sh"
done
This script will collect all job names related to jobs submitted by <username> in $jobnames and then iterate over the ones it should expect. If one is not found, grep will return non-zero exit code and the || sbatch will be executed.
Replace the three lines in your cron file with a single one running the above script.
I have a script that I would like to run 60 seconds after initial system reboot and then every 10 minutes after that. I currently need two cron job listings to achieve this:
*/10 * * * * php myscript.php
#reboot /bin/sleep 60; php myscript.php
The first listing will run my cron job immediately after system boot and so I need to have the second listing to account for the on start wait time.
Is there anyway to combine the above two cron listings into one?
I want to schedule a cron job in Linux by running a shell script.
The scenario is, I'm taking the time in HH:MM format in the shell script from the user and want to schedule a cron job from the shell script. I even want the cron job to be executed only once.
Thanks in advance...
For the crontab, you can do something like this:
cur=$(crontab -l)
new="$mm $hh * * * your_command"
echo "$cur$new" | crontab -
For one-shot, crontab is not the good candidate. Use at (note: your_command must be a file, e.g., a bash script)
at -f your_command $hh:$mm
I want to get the details of the last run cron job. If the job is interrupted due to some internal problems, I want to re-run the cron job.
Note: I don't have superuser privilege.
You can see the date, time, user and command of previously executed cron jobs using:
grep CRON /var/log/syslog
This will show all cron jobs. If you only wanted to see jobs run by a certain user, you would use something like this:
grep CRON.*\(root\) /var/log/syslog
Note that cron logs at the start of a job so you may want to have lengthy jobs keep their own completion logs; if the system went down halfway through a job, it would still be in the log!
Edit: If you don't have root access, you will have to keep your own job logs. This can be done simply by tacking the following onto the end of your job command:
&& date > /home/user/last_completed
The file /home/user/last_completed would always contain the last date and time the job completed. You would use >> instead of > if you wanted to append completion dates to the file.
You could also achieve the same by putting your command in a small bash or sh script and have cron execute that file.
#!/bin/bash
[command]
date > /home/user/last_completed
The crontab for this would be:
* * * * * bash /path/to/script.bash
/var/log/cron contains cron job logs. But you need a root privilege to see.
CentOs,
sudo grep CRON /var/log/cron
I want to set a cron run after an other cron. For example: Cron A finishs at 01:00 PM, cron B will start at 01:01 PM. The problem is I don't know when cron A finishs.
I checked the crontab syntax. It doesn't provide any param for that purpose.
My actual situation is:
# This cron must run first.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
# two these crons runs at the same time.
0 13 * * * /usr/local/bin/php -f /path/update_user.php
0 13 * * * /usr/local/bin/php -f /path/update_image.php
# This cron runs right after two above cron completes.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
You can use the batch command inside the first cron to have the second thing being scheduled to run.
Your first job could produce a timestamp when finished.
Then you estimate - for example - that job A needs about 60 to 90 minutes. After 60 minutes, you start job B. Job b looks for the timestamp. If it is present, job B starts, else it waits for a minute and looks again.
After finishing, job B deletes the timestamp, or renames it, maybe from 'todo' to 'done'. You could insert the current date inside the file, to check, whether your estimation is still acceptable, or should be adjusted.
What I do in such cases (commonly a backup scenario where I don't want to thrash the disk by having concurrent backups) is to write a script that cron calls, and in the script have the actual tasks run serially.
Something like:
#!/bin/bash
/usr/local/bin/php -f /path/update_user.php
/usr/local/bin/someOtherTaskToRunSecond
YMMV.