LinuxQuestions.org
Visit Jeremy's Blog.
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 12-20-2016, 08:15 AM   #1
newhorizons009
LQ Newbie
 
Registered: Sep 2016
Posts: 18

Rep: Reputation: Disabled
crontab not executing the shell script.


Hello All,

I am trying to setup the cron on my system. For test, I am trying to run a shell script every minute.

Below is how my crontab is setup.
crontab -l
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /home/IM/test.sh

Below is my shell script.
cat /home/IM/test.sh
#! /bin/sh

echo "Hello there"

My crond is kicking every minute, I see,

crond: USER root pid 1683 cmd /home/IM/test.sh
crond: USER root pid 1685 cmd /home/IM/test.sh
crond: USER root pid 1687 cmd /home/IM/test.sh
crond: USER root pid 1689 cmd /home/IM/test.sh

But it seems that script is being executed. Any clues ?
 
Old 12-20-2016, 08:30 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
http://www.linuxquestions.org/questi...-script-36931/

You might want to add your SHELL and PATH variables to the script itself.

Is the script executable by the user running the cron job you show?
 
Old 12-20-2016, 08:40 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,596

Rep: Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164
There are other caveats besides the path that can trip up users when running scripts from cron. Basically when a script runs via cron it isn't connected to a terminal/display so it can't display messages.

Try the following. It will display a popup message via cron.
http://www.linuxquestions.org/questi...ronjob-937682/
 
Old 12-20-2016, 09:04 AM   #4
newhorizons009
LQ Newbie
 
Registered: Sep 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
There are other caveats besides the path that can trip up users when running scripts from cron. Basically when a script runs via cron it isn't connected to a terminal/display so it can't display messages.

Try the following. It will display a popup message via cron.
http://www.linuxquestions.org/questi...ronjob-937682/
Are you saying, that if the the script invoked by the cron has echo, that echo would *not* be flushed to the console ?

Last edited by newhorizons009; 12-20-2016 at 09:06 AM.
 
Old 12-20-2016, 09:05 AM   #5
newhorizons009
LQ Newbie
 
Registered: Sep 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
http://www.linuxquestions.org/questi...-script-36931/

You might want to add your SHELL and PATH variables to the script itself.

Is the script executable by the user running the cron job you show?
Yes, Script is executable.

Below is updated script after your comments. But still no echo output on the console.
#! /bin/sh
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local/bin

/bin/echo "Hello there"
 
Old 12-20-2016, 09:07 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,604

Rep: Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604Reputation: 5604
crond has no console, it will not be able to echo anything (and jobs initiated by crond will inherit its state, so they will have no console too).
 
Old 12-20-2016, 09:23 AM   #7
newhorizons009
LQ Newbie
 
Registered: Sep 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
crond has no console, it will not be able to echo anything (and jobs initiated by crond will inherit its state, so they will have no console too).
Awesome info. I ran crond in foreground and bingo. I see the echo messages.
crond: USER root pid 1879 cmd /home/IM/test.sh
Hello there
crond[1877]: crond: USER root pid 1882 cmd /home/IM/test.sh
Hello there

I am going to move this in solved state.

Thanks everyone for pitching in.
 
Old 12-20-2016, 11:31 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
It isn't true you can't do echo from a cron job. What you can do is send the output of cron scripts to a log file.

* * * * * /home/IM/test.sh >>/tmp/test.log 2>&1

The echo you did would show up in that log (along with any errors should they occur). You could also set separate logging for individual commands within the script itself and also set stderr (file descriptor 2) to a separate log.

I notice you mention X11 as well though you're not actually using it here. For things that require an X display you'd have a similar problem because you're not on a display but you can use a tool called xvfb to run X sessions in the background.
 
  


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
Problem in executing script through crontab azheruddin Linux - Newbie 19 10-30-2015 12:46 PM
Crontab script not executing certain lines mkvakic Linux - Newbie 2 01-14-2013 04:33 AM
[SOLVED] Crontab not executing shell when date is added Jakkie Linux - Newbie 14 02-15-2012 05:18 AM
[SOLVED] Crontab not executing specific script abdoullah Linux - General 3 06-17-2011 02:15 PM
Seems crontab is not executing script... jonaskellens Linux - Newbie 3 11-18-2010 08:20 AM

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

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