LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-21-2021, 04:34 PM   #16
uteck
Senior Member
 
Registered: Oct 2003
Location: Elgin,IL,USA
Distribution: Ubuntu based stuff for the most part
Posts: 1,177

Rep: Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501Reputation: 501

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.
 
Old 01-21-2021, 04:39 PM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,766

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
Quote:
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?

Last edited by michaelk; 01-21-2021 at 04:41 PM.
 
Old 01-21-2021, 06:27 PM   #18
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by n00b_noob View Post
I changed my cron to:
Code:
*/1 * * * * /usr/bin/App1 && /usr/bin/echo "App1 done"
Then checked the "cron" file:
Code:
# 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.
 
Old 01-21-2021, 06:47 PM   #19
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by petelq View Post
How about changing the time to 5 minutes into the future and then sit and watch for 5 minutes? Change time back after. Just a thought.
Quote:
Originally Posted by jmgibson1981 View Post
Thought I was the only one that did this. I never thought of any other way. Just wait and see works fine.
Definitely not...have used this method “forever”...soon learned that 5 minutes was to long, however I’m just not that patient.

That said, my system logs all cron runs in /var/log/cron

Last edited by scasey; 01-21-2021 at 06:48 PM.
 
Old 01-25-2021, 03:55 PM   #20
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,813

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by uteck View Post
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.
 
Old 01-27-2021, 05:57 PM   #21
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,407
Blog Entries: 3

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
For a quick look I usually redirect output to a notification if fail

Code:
sh My_Script.sh || DISPLAY=:0 notify-send --urgency=critical "FAIL" "no luck for script in crontab"
 
Old 02-02-2021, 06:55 AM   #22
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by uteck View Post
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.
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done:"`date +"%Y%m%d"` >> /var/log/myapps.log'
You could also look into the proper way to redirect errors thrown by cron so they would also get logged.
Thank you so much.
File created, but not with the date and time:
Code:
App1 done done at
Why?
 
Old 02-02-2021, 07:02 AM   #23
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,766

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
% are chron special characters and need to be escaped i.e. add a backslash \ before it.
 
Old 02-03-2021, 02:42 AM   #24
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
% are chron special characters and need to be escaped i.e. add a backslash \ before it.
Excuse me, I used below form:
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done at $(\usr/bin/date)" >> /var/log/CronApp.log
How can I fix it?
To fix previous form, I must correct it as below?
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done:"date +"\%Y\%m\%d" >> /var/log/CronApp.log

Last edited by n00b_noob; 02-03-2021 at 02:49 AM.
 
Old 02-03-2021, 03:35 AM   #25
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,766

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
That is correct.
 
Old 02-03-2021, 12:05 PM   #26
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by n00b_noob View Post
Excuse me, I used below form:
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done at $(\usr/bin/date)" >> /var/log/CronApp.log
How can I fix it?
To fix previous form, I must correct it as below?
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done:"date +"\%Y\%m\%d" >> /var/log/CronApp.log
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"
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done: $(/usr/bin/date +"%Y-%m-%d")"  >> /var/log/CronApp.log
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.
 
Old 02-12-2021, 12:41 PM   #27
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
That is correct.
No!
I used below syntax:
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done:" date + "\%Y\%m\%d" >> /var/log/CronApp.log
And the output is:
Code:
App1 done: date + %Y%m%d
 
Old 02-12-2021, 01:18 PM   #28
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by n00b_noob View Post
No!
I used below syntax:
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done:" date + "\%Y\%m\%d" >> /var/log/CronApp.log
And the output is:
Code:
App1 done: date + %Y%m%d
If you read my post #26 above you would see the change I made that allows that to work correctly.
 
Old 02-12-2021, 01:24 PM   #29
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,766

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done:" date + "\%Y\%m\%d" >> /var/log/CronApp.log
That is because you did not use the exact syntax of the provided cron job.

Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done: $(/usr/bin/date +"\%Y-\%m-\%d")"  >> /var/log/CronApp.log
too late...

Last edited by michaelk; 02-12-2021 at 01:25 PM.
 
Old 02-13-2021, 08:28 AM   #30
n00b_noob
Member
 
Registered: Sep 2020
Posts: 436

Original Poster
Rep: Reputation: Disabled
I used below syntax and got the same result too:
Code:
00 23 * * 1 /usr/bin/App1 && /usr/bin/echo "App1 done: $(/usr/bin/date + "%Y-%m-%d" )"  >> /var/log/CronApp.log
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Debian daily cron job won't run, but does run in cron.hourly. sandersch Linux - General 7 05-24-2012 01:50 AM
[SOLVED] Configured Cron job executed every hour is instead executed every minute for 10m markings Linux - Software 4 05-13-2012 05:43 PM
[SOLVED] Xwindow's program will not run when executed on boot or when executed remotely richman1234 Programming 2 10-08-2010 01:32 PM
how can we know whether a Cron job has been executed or not ?? vikas027 Linux - Server 6 04-17-2009 01:49 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration