LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-31-2015, 06:51 AM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Rep: Reputation: Disabled
Command works fine in CLI but not in cron


The command I use to make a backup of the cron file is:
Code:
crontab -l > ~/readynas/Tim/IT/p5e/cron_$(date +%Y%m%d_%H%M).bak
Which when executed manually from the CLI generates a file called cron_20151031_1234.bak (depending on date & TOD of course) in the ~/readynas/Tim/IT/p5e folder. No issue whatsoever.
However when I put this in cron to execute this at midnight each night, nothing gets generated. extract from cron file below

Code:
# Backup Cron daily at midnight
0 0 * * * * crontab -l > ~/readynas/Tim/IT/p5e/cron_$(date +%Y%m%d_%H%M).bak
It's the exact same bit of code, but I am unsure why it's not being executed from the cron job. Do you know why this is not working please?

Kind regards
Tim
 
Old 10-31-2015, 07:46 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
all i know about cron is that it doesn't use $PATH, so you have to give absolute paths to everything.

i would change '~' to /home/yourusername and 'date' to '/usr/bin/date' (although date is possibly located elsewhere).
 
Old 10-31-2015, 10:11 AM   #3
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
all i know about cron is that it doesn't use $PATH, so you have to give absolute paths to everything.

i would change '~' to /home/yourusername and 'date' to '/usr/bin/date' (although date is possibly located elsewhere).
Thanks for the reply. It did not fix the issue unfortunately. Perhaps I can copy the file itself, do you know where it is stored?

Many thanks
Tim
 
Old 10-31-2015, 10:22 AM   #4
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
First, cron does not log in, thus there are no environment variables set for it, it certainly is not aware of your user environment. Thus the need to use full paths. Second, for the very same reason it will not be able to access remote shares by default.
 
Old 10-31-2015, 11:17 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Check out the gotchas in the following link. Should help you find the answer to your problem.
http://www.pantz.org/software/cron/croninfo.html

A users crontab does have a very limited default environment but can be changed easily. For example:
Quote:
HOME=/home/user
LOGNAME=user
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
PWD=/home/user
 
Old 10-31-2015, 11:46 AM   #6
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Check out the gotchas in the following link. Should help you find the answer to your problem.
http://www.pantz.org/software/cron/croninfo.html

A users crontab does have a very limited default environment but can be changed easily. For example:
OK great thanks. Got it working now. Was an interesting issue. Good to find out why it was not playing ball.
 
  


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
[SOLVED] convert works from CLI, but doesn't work when cron'd in a .sh anon091 Linux - Newbie 11 10-21-2013 02:58 PM
CUPS web interface: IPP printer queue cannot be started (CLI works fine) lefty.crupps Linux - Software 0 04-07-2010 11:37 AM
Command not working in cron job, but works fine in root Roosta21 Linux - Software 4 11-22-2007 08:08 AM
php script will not run correctly as cron, fine from cli dtra Linux - Software 2 12-22-2005 07:21 AM
cron issue, php script runs fine in browser and cli dtra Linux - Software 1 07-15-2005 05:45 AM

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

All times are GMT -5. The time now is 03:49 PM.

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