LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-07-2015, 01:50 AM   #1
twallstrom
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
Script not working when run via cron


I have a baffling issue with a simple script I wrote that, when run, will send an E-mail with my current ip address.
I have configured crontab to execute the script which runs but does not end up sending the e-mail.
If I run the same script manually (./script) the e-mail gets sent.
I write to a log each time the script runs so I know that cron is executing the script.

Any tips what might cause this is greatly appreciated.

Last edited by twallstrom; 06-07-2015 at 01:55 AM.
 
Old 06-07-2015, 02:40 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If you search LQ for this then usually it's down to using full paths and such. If you want to troubleshoot the cause yourself then make your cron job be a BASH script (see first line of script) then make the second line read "set -vx" (w/o the double quotes), have cron execute it, then check root (or rather the unprivileged users mail account that receives root) email for clues, elif post your script in full (and preferably between vBB code tags).
 
1 members found this post helpful.
Old 06-07-2015, 04:45 AM   #3
brebs
Member
 
Registered: May 2013
Posts: 89

Rep: Reputation: Disabled
If using e.g. cronie, then put at the top of your crontab something like:

Code:
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
Old 06-08-2015, 02:55 AM   #4
twallstrom
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
It was a PATH issue. When I provided the full path to the command (ssmtp) it worked like a charm.
Still I do not understand why the it worked when I was logged in via SSH and ran the command manually (./script) but not with Cron? I assume cron is not run as my user even though it was configured with that same user.

Thanks!
 
Old 06-08-2015, 03:25 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,925

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
I think that was already explained, cron has its own environment and that is restricted, so it won't find those commands for you (but you need to specify them using full path).
 
1 members found this post helpful.
Old 06-08-2015, 01:33 PM   #6
brebs
Member
 
Registered: May 2013
Posts: 89

Rep: Reputation: Disabled
Quote:
Originally Posted by twallstrom View Post
Still I do not understand why
Because SSH runs ~/.profile or ~/.bash_profile or ~/.bashrc, whatever it is. And one of those scripts sets up your PATH.
 
  


Reply

Tags
cronjobs, email, scripting



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
working script fails when run by cron adayforgotten Linux - Server 8 04-03-2013 12:50 PM
[SOLVED] Script not working in cron but working fine manually jasperux Linux - Newbie 2 07-04-2012 11:28 PM
script run fine from ./ but when put in cron does not run j-me Linux - Server 6 06-16-2012 10:02 AM
Script runs Manually but not in Cron, yet other parts of the script run mccartjd Linux - Newbie 5 01-08-2012 06:54 PM
can't get cron to run my script. dr_zayus69 Linux - Software 6 04-12-2005 06:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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