How to check if appened cron job has no errors while executing? - cron

I have added a three different executable scripts - bash, php, python scripts on to a crontab as cronjob
How would I check that three executable scripts are not giving any errors or not having any errors while executing from cron tab ?
Thanks !!

Not sure I understand the question, is this 3 different cron entries, or is three scripts sharing the same cron entry? For the latter (one cron entry for three scripts), do this (for a cron run every minute):
* * * * * /bin/bash script.sh && /usr/bin/php script.php && /usr/bin/python script.py
The php will only run if the bash succeeds. Likewise, the python will only run if both the bash and php script succeed. If you need all scripts to run regardless if one of them fails, have each job run from their own cron entry. In which case, do this:
* * * * * /bin/bash script.sh 2>>/var/log/script.sh.errors && echo "Bash Completed Successfully" >>/var/log/script.sh.success
* * * * * /usr/bin/php script.php 2>>/var/log/script.php.errors && echo "PHP Completed successfully" >>/var/log/script.php.success
* * * * * /usr/bin/python script.py 2>>/var/log/script.py.errors && echo "Python Completed successfully" >>/var/log/script.py.success
In the second example, errors log to a separate file. Scripts can return a exit code 0 (i.e. "Success") even if there are errors (for instance, if the script actually has an exit 0 statement at the end of it). Thus, it's best to try to capture errors as the script runs, then capture a successful exit code, if it exits successfully.

Related

Docker cron scheduled job not running

I am trying to use a docker container based on an Alpine image to run a scheduled cron job, following this tutorial, but after printing the statement in my startup script, the container just exits, without running my other script.
My docker-compose service is configured as follows:
cron:
image: alpine:3.11
command: /usr/local/startup.sh && crond -f -l 8
volumes:
- ./cron_tasks_folder/1min:/etc/periodic/1min/:ro
- ./cron_tasks_folder/15min:/etc/periodic/15min/:ro
- ./cron_tasks_folder/hourly:/etc/periodic/hourly/:ro
- ./scripts/startup.sh:/usr/local/startup.sh:ro
So it runs an initial script called startup.sh and then starts the cron daemon. The startup.sh script contains the following:
#!/bin/sh
echo "Starting startup.sh.."
echo "* * * * * run-parts /etc/periodic/1min" >> /etc/crontabs/root
crontab -l
sleep 300
I dropped a sleep command in there just so I could launch an interactive shell on the container and make sure everything inside it looks good. The script creates another folder for 1min scripts. I have added a test script in there, and I can verify it's there:
/etc/periodic/1min # ls -a
. .. testScript
The script is executable:
/etc/periodic/1min # ls -l testScript
-rwxr-xr-x 1 root root 31 Jul 30 01:51 testScript
And testScript is just an echo statement to make sure it's working first:
echo "The donkey is in charge"
And looking at the root file in etc/crontabs, I see the following (I've re-run the container several times, and each time it's creating a new 1min folder, which is unnecessary, but I think not the problem here):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
The echo statement in testScript is never printed to my terminal, and the container exits with exit code 0 shortly after starting. I want to print this statement every minute... what am I missing?
In the docker compose file you have
command: /usr/local/startup.sh && crond -f -l 8
The intention is to run as a shell command, but it's not at all clear from the question that's what's going to happen; that depends on your ENTRYPOINT. Since it's defined with [] brackets, not additional shell will be provided. The command value will be passed as arguments to the ENTRYPOINT.
Assuming that will become a shell command, && in the shell runs the left hand side, and if that succeeds, then runs the right hand side. So startup.sh needs to complete before crond is executed. startup.sh ends with
sleep 300
crond is invoked only after that 300 seconds.
In either case, crond is either not invoked at all, or sleep has not been completing. The comments show that an error starting crond was discovered.
Using an entrypoint such as this is standard practice to configure the environment before, or provide runtime parameters when, invoking the main executable. To do it right, you should make sure to use exec to run the main executable so that it receives the signals that would otherwise go to the bash shell running the entrypoint script.
So at the end of startup.sh:
exec crond -f -l 8
Will replace the shell running startup.sh with crond, so that crond receives all signals (at this point the shell is gone). It's subtle but important!
In general, keep the invocation of the application as simple as possible. Case in point, your execution process was split between entrypoint, command, and startup script, with no clear interface between them. You wouldn't have gotten hung up on the invocation if you had put crond directly into the Dockerfile and left it at that. Sometimes arguments must be provided at runtime, but environment variables - which have names, not just positions - are often preferred. This keeps invocations simple and debugging straightforward. But, when that doesn't work, a shell script entrypoint is a great solution - just make sure to exec your final process!

Cronjobs do not run

I'm trying to run a cronjob to start and stop a server under a non-sudo user. I've tried asking others and doing what I saw from looking on google before asking here, but I'm still stuck.
Here's what's in my crontab for the server user:
* * * * * /home/server/startup/stop.sh
* * * * * /home/server/startup/start.sh
Here is what is in my stop.sh script:
#! /bin/sh
screen -r server -X quit
Everything runs normally if I run it using sh, and I only encounter a problem when using cron.
From what I see there could be 2 possible problems:
If the lines you are running in crontab are (and only those):
home/server/startup/stop.sh
home/server/startup/start.sh
then you are missing the time part of the line. If you want to run your program only once on boot you can run:
#reboot home/server/startup/start.sh
You are not giving the full path to your program (possibly you are just missing a / in the begging). Try running
* * * * * /home/server/startup/start.sh
or
#reboot /home/server/startup/start.sh
If these don't work I recommend you try the following to troubleshoot the issue:
Run the command using sh in the cron:
* * * * * /bin/sh /home/server/startup/start.sh
Try redirecting the stdout and stderr of your command to a file and see if any errors occur

CentOS Unix Cron order of execution when all set to same time

I have a series of cron jobs running at the command line calling the php interpreter all by the same user configured to run once a day
0 0 * * * /usr/bin/php -q /mydirectory/myphp.php
0 0 * * * /usr/bin/php -q /mydirectory/myphp2.php
0 0 * * * /usr/bin/php -q /mydirectory/myphp3.php
Do these all execute at once or do the execute in the order of entry in some cron table, complete and move on to the next cron job?
And to answer your question despite the off-topic:
They will get executed in parallel, not sequentially. If you need some order it would pay to add them all to one script, and execute them sequentially separated by &&, e.g.
#!/bin/bash
/usr/bin/php -q /mydirectory/myphp.php && /usr/bin/php -q /mydirectory/myphp2.php && /usr/bin/php -q /mydirectory/myphp3.php

Cron job does not run in RHEL

I'm running RHEL and I'm trying to set up a cron job to run a shell script every 5 minutes.
Following the directions here: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-Automating_System_Tasks.html#s2-configuring-cron-jobs
I have service crond start and chkconfig crond on. Then I edited /etc/crontab and added:
*/5 * * * * my-user /path/to/shell.sh
I did a chmod +x shell.sh. And I made sure to add a new line character at the end.
I'm expecting it to run every 5 minutes but it never executes.
What am I doing wrong?
Simply try to add the cronjob entry and check the script is working fine or not by taking the viewable output in the script.
echo "test time - $(date)" > script.sh
chmod +x script.sh
crontab -e
Then enter the cronjob as below,
*/5 * * * * sh /path/to/script.sh > /path/to/log.file
Check if the log is writing correctly. If its fine, better cross check the script that you are trying to execute via cron. Otherwise it will be a cron issue.

How to reboot via cron on scheduled basis. Ubuntu 14.04

I have a very simple script that works from the command line.
#!/bin/bash
reboot
When I put a call to execute the script into root users crontab -e using the following format it does not run. It does run the first two commands, just that last one is giving me grief. I have no MTA installed as I do not need it.
*/10 * * * * service jwtpay restart
0 3 * * * bash /root/backup/mongo.backup.s3.sh kickass /root/backup >/dev/null 2>&1
0 */3 * * * bash /root/reboot.sh >/dev/null 2>&1
What am I missing?
Maybe the script is not executable... Since you use root's crontab why call the binary via a script and not the binary itself? Use the full path to the binary. It may vary on your system. Find out where it is with which reboot.
0 */3 * * * /sbin/reboot
Don't forget to restart the cron daemon, after changeing the crontab.

Resources