LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 06-15-2019, 02:27 AM   #1
Dain_Bramage
Member
 
Registered: Mar 2019
Posts: 46

Rep: Reputation: Disabled
Cron job not running


I have two cron jobs in my (not root) crontab.

Code:
0 2 * * * /home/mike/Scripts/rsync.sh
0 19 * * * /home/mike/Scripts/zfs.sh 2>&1 /tmp/testlog.log
The first one works, the second one does not.

If I run

Code:
/home/mike/Scripts/zfs.sh
in terminal it works. How can this be? Both scripts are owned by me.

The output of

Code:
cat /var/log/syslog | grep zfs.sh
is

Code:
Jun 15 19:00:01 Ubuntu19-04 CRON[15434]: (mike) CMD (/home/mike/Scripts/zfs.sh 2>&1 /tmp/testlog.log)
but there is no testlog.log file generated in the /tmp folder.
 
Old 06-15-2019, 03:59 AM   #2
Dain_Bramage
Member
 
Registered: Mar 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
So I added

Code:
touch /home/mike/test
as the first line of the script then ran it as a cronjob. The "test" file was created.

The next line of my script, which does not run as a cronjob but which will run in terminal is:

Code:
zfs snapshot storage/Back-ups/Business-computer@$(date +%d%B%Y)
Why will this run in terminal but not as a cronjob in a script?
 
Old 06-15-2019, 04:12 AM   #3
Dain_Bramage
Member
 
Registered: Mar 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
I ran a dummy cronjob:

Code:
* * * * * env > /tmp/env.output
In /tmp/env.output it said

Code:
SHELL=/bin/sh
If I run

Code:
env | grep SHELL
in terminal it reports

Code:
SHELL=/bin/bash
I have

Code:
#!/bin/bash
at the beginning of the script and have added

Code:
SHELL=/bin/bash
to the beginning of my crontab.

After making this change and running the dummy crontab job again the output of "/tmp/env.output" is now


Code:
SHELL=/bin/bash
LANGUAGE=en_NZ:en
PWD=/home/mike
LOGNAME=mike
HOME=/home/mike
LANG=en_NZ.UTF-8
SHLVL=1
PATH=/usr/bin:/bin
_=/usr/bin/env
Cronjob still not running.

Last edited by Dain_Bramage; 06-15-2019 at 05:30 AM.
 
Old 06-15-2019, 06:35 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Use the full path to the zfs command in your script. I believe the zfs command is in /sbin which is not in the cron default path environment as posted above.

Since /sbin is in the users path environment it works from the command line but not from cron.

Last edited by michaelk; 06-15-2019 at 06:37 AM.
 
Old 06-15-2019, 06:38 AM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Where is zfs located?
Put the absolute path the zfs in your script.
 
Old 06-15-2019, 12:34 PM   #6
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,801

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Dain_Bramage View Post
I have two cron jobs in my (not root) crontab.

Code:
0 2 * * * /home/mike/Scripts/rsync.sh
0 19 * * * /home/mike/Scripts/zfs.sh 2>&1 /tmp/testlog.log
The first one works, the second one does not.

If I run

Code:
/home/mike/Scripts/zfs.sh
in terminal it works. How can this be? Both scripts are owned by me.
Permissions are not the problem.

If you wanted to send the output of zfs.sh into that log file in /tmp, I'd use the crontab entry:
Code:
0 19 * * * /home/mike/Scripts/zfs.sh >/tmp/testlog.log 2>&1
Note the ">". Without the redirection how would the output make it into that file? I suspect that cron is just silently skipping the job as it doesn't understand the command. If the log file name was an argument ($1) to the script, I cannot predict what it would do if it saw "2>&1" in that location on the command line.

Another reason scripts will work on the command line but not when run under cron is your environment. Scripts run via cron do not run your .bashrc, .profile, or whatever other files you're sourcing to configure your interactive environment. A lot of the time cron jobs fail because of the PATH not being correct. Again, whatever you do in your account's profile setup is not being done when running under cron so if you have amended your PATH to include locations not in the system-wide default PATH and your script needs to know about those, your script will fail. NOTE: I can't recall not getting an email with a "file not found"-type error message in those cases. You can define/recreate your interactive PATH with a "PATH=" line near the top of your crontab; no need to 'export' it.

I dealt with this years ago by writing my .profile to have a section for things I wanted set up when working interactively, a section for non-interactive sessions, and a session for either. (I hesitate to even mention this, though, as it generated a mini flame war on another forum many, many moons ago when I mentioned it in response to someone's cron problems.) Anyhoo, the key was defining:
Code:
INTERACTIVE=$( false )
if [ -t 0 ]; then     # Is stdin connected to a terminal/keyboard?
   INTERACTIVE=$( true )
fi
For example, I wouldn't need my aliases to be defined unless I'm at a real terminal ( if $INTERACTIVE then source aliases and do other niceties like colored prompt, etc. ). Then I'd source my .profile in cron scripts to do all of the environment configuration---if I set up my .profile correctly, I had the same necessities in either console or cron. (A major driver for this at the time was my desire to have standardized message formats emitted from my scripts and I enforced that by using a set of shell messaging functions that I sourced in .profile; when issued non-interactively, the functions ensured that the messages were time stamped. I should note that I don't do this so much any more though I still do source the messaging functions directly.)

HTH.

Cheers...
 
Old 06-16-2019, 06:54 AM   #7
Dain_Bramage
Member
 
Registered: Mar 2019
Posts: 46

Original Poster
Rep: Reputation: Disabled
Michael is correct, zfs is in /sbin which is not in the cron default path environment as posted.

I added

Code:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
to my crontab file. It seemed more efficient to do it that way rather than add the relevant path to each script individually.

I am happy to report the script is now working in the cronjob and I am also happy to understand why it wasn't working.

I can also report that rnturn's suggested amendment to the cronjob resulted in the "/tmp/testlog.log" file being created with some output which will be useful should I run into problems in the future.

Thank you to everyone for your help, I really appreciate it.
 
  


Reply

Tags
cronjob



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
Cron Job Not Running - Looks Like Cron Tried Noble User Linux - Newbie 7 10-26-2014 10:26 AM
how to abort cron if the previous cron job not yet finished? Winanjaya Linux - Newbie 2 05-22-2012 06:44 PM
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 08:52 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
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 01:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 01:38 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