LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-11-2019, 06:48 AM   #1
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Rep: Reputation: 29
updated crontab with new cronjob: old jobs run, new job does not


Using Centos 7
The new cronjob is the last command and it does not run at its stated date/time -
crontab -l
Code:
10 8 * * 1-5 /root/bashscript >/dev/null 2>&1
#
10 11 * * 1-5 /root/bashscript >/dev/null 2>&1
#
10 14 * * 1-5 /root/bashscript >/dev/null 2>&1
#
10 17 * * 1-5 /root/bashscript >/dev/null 2>&1
#
10 20 * * 1-5 /root/bashscript >/dev/null 2>&1
#
10 23 * * 1-5 /root/newbashscript >/dev/null 2>$1
#
This is "newbashscript" -
Code:
#/bin/bash
rsync --bwlimit=500 -aAvz --log-file=/var/log/hellolog.txt /mnt/dir/ root@xxx.xxx.xxx.xxx:/mnt/dir/
1. "newbashcript" uses an rsync command similar to all the other "bashscript" commands.

2. "newbashcript" when executed manually from shell completes operation and is confirmed by log file.

3. All "bashscript" have similar rsync commands and execute operation at the designated time with no error; confirmed by logfiles.

4. All scripts in /root have permissions set as -
Code:
-rwx------.  1 root root
Yet, I'm still missing something because newbashcript will not run as a cronjob.
Do you see a problem or error that I'm overlooking?
I don't know what next to try in troubleshooting.
Thanks for your time and help.
 
Old 01-11-2019, 06:58 AM   #2
dc.901
Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS 6-7; SuSE 8-12
Posts: 660

Rep: Reputation: 199Reputation: 199
If you run this script manually, does it run?

Couple of things:
In your script, I see:

Code:
#/bin/bash
It should be

Code:
#!/bin/bash (your script is missing "!")
The way I troubleshoot cron jobs is to redirect to a file like:
Code:
10 23 * * 1-5 /root/newbashscript >> /tmp/newbashscript_job.log 2>$1
Then, look at the log file (/tmp/newbashscript_job.log) after the job runs (or supposed to run) to see if it points out anything.
When I know job is running as desired, then redirect to /dev/null (as you have).
 
Old 01-11-2019, 07:52 AM   #3
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by dc.901 View Post
If you run this script manually, does it run?

It should be

Code:
#!/bin/bash (your script is missing "!")
I'm not sure I follow -

The bash script runs when executed manually from shell as stated in "2." from original post.
I haven't had a problem with the following format in bash scripts -
Code:
#/bin/bash
# add commands starting on next line
[QUOTE=dc.901;5947591]
The way I troubleshoot cron jobs is to redirect to a file like:
Code:
10 23 * * 1-5 /root/newbashscript >> /tmp/newbashscript_job.log 2>$1
I'll give this a try.
Thanks for your help, definitely appreciate the feedback.
 
Old 01-11-2019, 08:54 AM   #4
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 274

Rep: Reputation: Disabled
The first line in your script should be a 'shebang', i.e. a #!, not a # which only makes it a comment.

Second, in cron jobs use full paths and do not rely on $PATH values. Cron's version of the default path is not the same as the version of your path when you run the script manually. So your rsync command should be /usr/bin/rsync not just rsync. The path can be set in a variable in the crontab file but you are better off using full paths in the commands and the various scripts.
 
1 members found this post helpful.
Old 01-11-2019, 11:00 AM   #5
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,180

Rep: Reputation: 662Reputation: 662Reputation: 662Reputation: 662Reputation: 662Reputation: 662
Quote:
Originally Posted by Sum1 View Post
Yet, I'm still missing something because newbashcript will not run as a cronjob.
Just to make sure: you did use the crontab command to install the changed list of "things to do" for cron? There are so many different implementations of crond out there that it is hard to suggest other ways of telling crond that "one of its tables has changed" but the corresponding crontab command surely does know how to do that.
Do not ever edit a crontab file directly!

And I don't really think that it is needed to use bash for most cron jobs, but if: the script does have to start with #!/bin/bash if you are using bash'isms in it as the default for cronjobs is /bin/sh
 
Old 01-11-2019, 01:58 PM   #6
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by agillator View Post
The first line in your script should be a 'shebang', i.e. a #!, not a # which only makes it a comment.

Second, in cron jobs use full paths and do not rely on $PATH values. Cron's version of the default path is not the same as the version of your path when you run the script manually. So your rsync command should be /usr/bin/rsync not just rsync. The path can be set in a variable in the crontab file but you are better off using full paths in the commands and the various scripts.
Doh! OK --- now i see what DC.901 was telling me.
I don't see how any of my scripts are working because all of them start with
Code:
#/bin/bash
-- they are all missing the "!"
All but the "newbashscript" work in the root crontab.
Now I've got a mystery on top of a non-function. :-)

I'll update all the scripts with "#!" and add full paths for the rsync command.

Thanks for the review!
 
Old 01-11-2019, 02:05 PM   #7
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by ehartman View Post
Just to make sure: you did use the crontab command to install the changed list of "things to do" for cron?
Hi, and definitely Yes.
I've made that mistake in the past and learned not to do that.

While logged in as root, I do
Code:
crontab -e
to edit the root crontab file.
I complete the edits, write to file, and quit --
Code:
:wq
Thanks for guidance here too.
 
Old 01-11-2019, 04:55 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,758

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Quote:
Originally Posted by agillator View Post
The first line in your script should be a 'shebang', i.e. a #!, not a # which only makes it a comment.

Second, in cron jobs use full paths and do not rely on $PATH values. Cron's version of the default path is not the same as the version of your path when you run the script manually. So your rsync command should be /usr/bin/rsync not just rsync. The path can be set in a variable in the crontab file but you are better off using full paths in the commands and the various scripts.
This is a very good practice to get in.

As stated link the output to a file you are later read, but also add the following to the crontab -e

Code:
10 23 * * 1-5 /bin/bash /root/newbashscript >> /tmp/newbashscript_job.log 2>$1
should not be required as you have the #!/bin/bash in the 1st line of the script, but cannot hurt. you can also consider adding this to the first line:

Code:
#!/bin/bash -xvv
This will display in detail exactly what the script is doing and as you have the output going to a file, you can cat or more your way through the file to see in detail.
 
Old 01-13-2019, 02:04 PM   #9
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Original Poster
Rep: Reputation: 29
I've made the suggested changes to the bash script and it runs with no errors when executed manually.

Code:
[root@thing ~]# ./newbashscript
#!/bin/bash -xvv
/usr/bin/rsync --bwlimit=500 -aAvz --log-file=/var/log/newlog.txt /mnt/archive/ root@xxx.xxx.xxx.xxx:/mnt/archive/
+ /usr/bin/rsync --bwlimit=500 -aAvz --log-file=/var/log/newlog.txt /mnt/archive/ root@xxx.xxx.xxx.xxx:/mnt/archive/
sending incremental file list

sent 74,301,235 bytes  received 469,667 bytes  76,806.27 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.05
I set the cronjob to run on the 1st minute of every hour and there's no "newlog.txt" in the /tmp directory.
No sign that the cronjob is executing as expressed in the crontab.

Code:
[root@thing ~]# crontab -l
1 * * * * /bin/bash /root/newbashscript >> /tmp/newlog.txt 2>$1>
Not sure where to go . . . possibly delete the root crontab and make a new one?
 
Old 01-13-2019, 02:53 PM   #10
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,376

Rep: Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551
Code:
[root@thing ~]# crontab -l
1 * * * * /bin/bash /root/newbashscript >> /tmp/newlog.txt 2>$1>
I note that here, and in your OP, you have 2>$1 instead of 2>&1
Wouldn't that cause all the output to be redirected to a variable, and therefore disappear?

Also, here, there is a trailing redirection??
 
Old 01-13-2019, 08:53 PM   #11
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,180

Rep: Reputation: 662Reputation: 662Reputation: 662Reputation: 662Reputation: 662Reputation: 662
Quote:
Originally Posted by scasey View Post
Code:
[root@thing ~]# crontab -l
1 * * * * /bin/bash /root/newbashscript >> /tmp/newlog.txt 2>$1>
I note that here, and in your OP, you have 2>$1 instead of 2>&1
Wouldn't that cause all the output to be redirected to a variable, and therefore disappear?
Good catch, I didn't see that either.
It probably will resolt either in a cron error (see /var/log/cron), a bash error or even into the creation of a physical file '$1' in some default cron directory.
The trailing '>' just seems a typo, it wasn't there in his first post.
 
Old 01-13-2019, 09:35 PM   #12
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by scasey View Post
Code:
[root@thing ~]# crontab -l
1 * * * * /bin/bash /root/newbashscript >> /tmp/newlog.txt 2>$1>
I note that here, and in your OP, you have 2>$1 instead of 2>&1
Wouldn't that cause all the output to be redirected to a variable, and therefore disappear?

Also, here, there is a trailing redirection??
Thanks for this!
I'm updating the crontab and will report back.
 
Old 01-14-2019, 06:16 AM   #13
Sum1
Member
 
Registered: Jul 2007
Distribution: CentOS and Slackware and Gentoo
Posts: 313

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by scasey View Post
Code:
[root@thing ~]# crontab -l
1 * * * * /bin/bash /root/newbashscript >> /tmp/newlog.txt 2>$1>
I note that here, and in your OP, you have 2>$1 instead of 2>&1
scasey, thank you so much for your careful eye.
this morning i confirm the following found in /tmp/newlog.txt -

Code:
[root@thing tmp]# cat newlog.txt
sending incremental file list

sent 74,301,335 bytes  received 469,055 bytes  96,915.61 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.19
sending incremental file list

sent 74,301,407 bytes  received 469,127 bytes  100,161.47 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.15
sending incremental file list

sent 74,301,275 bytes  received 468,991 bytes  99,760.19 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.22
sending incremental file list

sent 74,301,415 bytes  received 469,131 bytes  101,797.88 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.15
sending incremental file list

sent 74,301,159 bytes  received 468,879 bytes  101,520.76 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.29
sending incremental file list

sent 74,301,047 bytes  received 468,763 bytes  99,892.87 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.35
sending incremental file list

sent 74,301,295 bytes  received 469,023 bytes  101,521.14 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.21
sending incremental file list

sent 74,301,199 bytes  received 468,907 bytes  100,972.46 bytes/sec
total size is 1,528,619,985,142  speedup is 20,444.27
sending incremental file list
The "newbashscript" clearly executed every hour overnight.

Kind regards!
 
Old 01-14-2019, 08:32 AM   #14
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,180

Rep: Reputation: 662Reputation: 662Reputation: 662Reputation: 662Reputation: 662Reputation: 662
Quote:
Originally Posted by Sum1 View Post
The "newbashscript" clearly executed every hour overnight.
So if everything is working alright now, please mark this thread as "Solved" (at the top, before post #1).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Scheduling important jobs with CRONTAB : Crontab Examples LXer Syndicated Linux News 0 09-11-2018 06:00 PM
New or updated OSs that can run on both very old and new HW LinksysWRT54G Other *NIX 6 03-28-2018 02:28 PM
Executable not found when run as crontab job RandomTroll Linux - Software 4 09-01-2017 02:36 PM
[SOLVED] crontab does not reread the crontab file upon modification of the latter. stf92 Slackware 4 08-01-2015 07:12 PM
the crontab job is not run ust Linux - Software 1 03-24-2008 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration