[SOLVED] How can I run a Cron job manually or how can I sure my job executed?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Ubuntu based stuff for the most part
Posts: 1,177
Rep:
From I know about the echo command as you are using it in your crontab, is that it will display that on the terminal since you did not specify anything else. Also, since the echo command is after the &&, it will only run if the previous command finishes with an exit code of 0.
Do you know how the app is exiting as that will determine if the echo command will run?
Since getting mail working just to see if the command is running may be a bit more effort, what about just echoing the line to a file in /var/log? That way you know it ran just by directing the output of echo to a file that you have log rotate deal with.
Jan 22 00:24:02 Server CROND[2343035]: (root) CMD (/usr/bin/App1 && /usr/bin/echo "App1 done")
cron ran the job. You will not see an App1 done message because cron is not associated with a terminal. You could write the message to a file somewhere like
/usr/bin/echo "App1 done" >> /tmp/app1.log
if you did not get the desired results then you will need to look at App1. Without knowing anything specific make sure that paths are absolute. Does the App require any global parameters?
# cat /var/log/cron
Jan 22 00:21:41 Server crontab[2342990]: (root) BEGIN EDIT (root)
Jan 22 00:23:21 Server crontab[2342990]: (root) REPLACE (root)
Jan 22 00:23:21 Server crontab[2342990]: (root) END EDIT (root)
Jan 22 00:24:01 Server crond[468693]: (root) RELOAD (/var/spool/cron/root)
Jan 22 00:24:02 Server CROND[2343035]: (root) CMD (/usr/bin/App1 && /usr/bin/echo "App1 done")
And:
Code:
# mail
No mail for root
Does app1 have anything that says to mail root when it successfully completes?
As has already been noted the echo command would echo to a terminal but with cron it has no where to go.
You might consider changing the echo command to a mail to command. or put that crontab line into /etc/crontab instead of the personal crontab for root.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,813
Rep:
Quote:
Originally Posted by uteck
From I know about the echo command as you are using it in your crontab, is that it will display that on the terminal since you did not specify anything else. Also, since the echo command is after the &&, it will only run if the previous command finishes with an exit code of 0.
Do you know how the app is exiting as that will determine if the echo command will run?
Since getting mail working just to see if the command is running may be a bit more effort, what about just echoing the line to a file in /var/log? That way you know it ran just by directing the output of echo to a file that you have log rotate deal with.
Unless the job is running under root's crontab, there will likely be permission problems creating breadcrumb files like that in /var/log. "/tmp" should work just as well during a debugging effort.
I'd spend some time figuring out why email isn't working. That's a prime tool (for me, anyway) to receive status reports, etc. without having to manually locate and inspect log files. Getting email working should be an essential first step when setting up the system. Local emails should be a snap to have working without a lot of fuss.
There's a LOT of ways to leave evidence behind that a cron job has run. If this is still a problem for the OP, I suggest using:
Code:
$ sudo tail -f /var/log/messages
if the OP's system is configured to use traditional syslog logging or:
Code:
sudo journalctl --follow
if systemd's logging is being used, to watch for log entries indicating that a cron job has been run.
Using the "logger" command in the script that's being run via cron can be helpful---you'll see the logger message in the system logs.
Code:
JOBNAME=$0 # Or something you can easily grep for
logger -p local0.notice -t ${JOBNAME} "Script started."
...
logger -p local0.notice -t ${JOBNAME} "Script finished."
My experience with failing cron jobs is that some assumption about the environment is not valid and the script runs interactively but not when run under cron. Once email is working properly, even those failures ought to result in an email about the failure. Try:
Code:
* * * * * (date; env) 2>&1 # comment me out once email is working
which is handy in that it shows the environment that is defined for cron job---probably not what the user has set up when working at a terminal. Any script you run that needs more than what you get back in the output of the above cron job will have to include those missing items.
Aside: Running "crontab -e" is, IMHO, a bad way to manage one's cron jobs. You're one major problem requiring a re-install away from losing your cron job schedule. Keep it somewhere under $HOME and use a command like "crontab ~/my_master_crontab_file" to update the cron schedule when needed.
Your echo command would display on the terminal, but that does not do anything when run from cron. You need to redirect the echo to a log file you make.
I would change that last line to the below. The way you have it the output would be "App1 done:date +\%Y\%m\%d" while my change makes it "App1 done: 2021-02-03"
and it should work fine. If you want the full date with day, time, and day of week format the simple date command without the formatting as $(/usr/bin/date) will work as well and would give you "App1 done: Wed Feb 3 12:02:47 PM CST 2021"
Your earlier line would have worked except for the \ instead of / in the date command
You will also need to make certain the log file is able to be written to by the user running this cron job or the logging will still fail.
Note that your command only logs a success of the backup. If you also want to log failure then a slight change to the command will do that as well.
Code:
00 23 * * 1 /usr/bin/App1; if [ $? ]; then /usr/bin/echo "App1 done at $(/usr/bin/date)" >> /var/log/CronApp.log; else /usr/bin/echo "App1 FAILED at $(/usr/bin/date)" >> /var/log/CronApp.log; fi
This way you get a log entry every time the job runs telling you the status instead of a missing entry when it fails
Last edited by computersavvy; 02-03-2021 at 12:45 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.