LinuxQuestions.org
Visit Jeremy's Blog.
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 12-03-2018, 07:09 AM   #1
atulkvtiwari
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Rep: Reputation: Disabled
Crontab not executing my shell script its executing standlaone


Hi I am new to linux i have atul.sh script to add route its working standalone but not working in crontab can anyone help
 
Old 12-03-2018, 07:17 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,138

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
Make sure any commands you use have /the/full/path/to/the/command as the cron environment doesn't get all paths.
 
Old 12-03-2018, 07:28 AM   #3
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,545

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
Quote:
Originally Posted by atulkvtiwari View Post
Hi I am new to linux i have atul.sh script to add route its working standalone but not working in crontab can anyone help
Yes

Ok, that answered your question, now I want to talk about cron and crontab.
Crontab is the tool to edit your crontab file. It does not execute your cron processes or scripts, it just edits the file. The program that DOES execute your script is named cron. Cron reads your crontab file and evaluates the schedule information to decide when to run the command provided. When the time parameters are satisfied, it calls /bin/sh to run your command.

Note that last, cron does not call /bin/bash or your shell, it calls the system shell. I must add, it cron runs under root authority, and runs the command from a crontab file under the authority of the owner of that file. This is very correct, but cron dos NOT load up the environment the user would have if they log in, just the default environment.

As a result there are these things one should ensure in a shell script one wishes to run from the crontab file:
#1 it must have execute permissions. (I suspect this does, or you could not run the script at the command line.)
#2 It should be called using the full path name, as cron may not otherwise find it for execution.
#3 It should start with a she-bang "#!" setting the correct shell, if "sh" is not the proper one to use.
#4 either make every external call in the script use the full path to the executible, or set the PATH environment variable early in the cript - before making any calls.

There are other things that can cause a problem, but these address the most common.
Now: we are left guessing about the contents of your crontab file. Could you please quote your crontab file (or just the one line containing this script as a command) so that we might consider if that line is correct and functional in the manner you desire?
 
1 members found this post helpful.
Old 12-03-2018, 07:36 AM   #4
atulkvtiwari
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Make sure any commands you use have /the/full/path/to/the/command as the cron environment doesn't get all paths.
Hi i have given the full path kindly see my crontab lines
* * * * * root /root/Desktop/atul.sh
where i have kept my script wit chmod +x atul.sh
 
Old 12-03-2018, 07:41 AM   #5
atulkvtiwari
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
Yes

Ok, that answered your question, now I want to talk about cron and crontab.
Crontab is the tool to edit your crontab file. It does not execute your cron processes or scripts, it just edits the file. The program that DOES execute your script is named cron. Cron reads your crontab file and evaluates the schedule information to decide when to run the command provided. When the time parameters are satisfied, it calls /bin/sh to run your command.

Note that last, cron does not call /bin/bash or your shell, it calls the system shell. I must add, it cron runs under root authority, and runs the command from a crontab file under the authority of the owner of that file. This is very correct, but cron dos NOT load up the environment the user would have if they log in, just the default environment.

As a result there are these things one should ensure in a shell script one wishes to run from the crontab file:
#1 it must have execute permissions. (I suspect this does, or you could not run the script at the command line.)
#2 It should be called using the full path name, as cron may not otherwise find it for execution.
#3 It should start with a she-bang "#!" setting the correct shell, if "sh" is not the proper one to use.
#4 either make every external call in the script use the full path to the executible, or set the PATH environment variable early in the cript - before making any calls.

There are other things that can cause a problem, but these address the most common.
Now: we are left guessing about the contents of your crontab file. Could you please quote your crontab file (or just the one line containing this script as a command) so that we might consider if that line is correct and functional in the manner you desire?
Hi i have to add two routes when system reboots or this routes disappear first line is #!/bin/sh and second line this route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X it executes manually running atul.sh but in crontab its not executing so kindly help me
 
Old 12-03-2018, 07:48 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,138

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
Quote:
Originally Posted by atulkvtiwari View Post
Hi i have given the full path kindly see my crontab lines
* * * * * root /root/Desktop/atul.sh
where i have kept my script wit chmod +x atul.sh
Which is nothing to do with what I said. I'll say it again, make sure any commands you use in your script use the full path to the command.
 
Old 12-03-2018, 07:53 AM   #7
atulkvtiwari
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Which is nothing to do with what I said. I'll say it again, make sure any commands you use in your script use the full path to the command.
All the path in script is working normal when i run from terminal it executes properly just i want to schedule this script for regularly in minutes
 
Old 12-03-2018, 08:25 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,138

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
I'll take a guess that your script is in bash, if that's the case add a line like this:
Code:
#!/bin/bash
set -x
then at the end of your cron entry add
Code:
>> /root/Desktop/myscript.log
check /var/log/cron to ensure your job runs and then check the output in the myscript.log file for any clues.

Also feel free to post your script here using the CODE tags
 
Old 12-03-2018, 08:36 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,845

Rep: Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743Reputation: 3743
Welcome to LinuxQuestions.

What linux distribution/version are you running? A users crontab will always run as that user so specifying root does not work. Specifying the user is only required for a system cron job i.e /etc/crontab.

Code:
 * * * * * /root/Desktop/atul.sh
A cron job has a different environment path then its user so you should also add the full path to the command inside the script. Although I believe that roots cron does include /sbin.

Code:
#!/bin/bash 
/sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X
It would be better to save the route using a configuration script but that depends on your distribution. When does the route disappear?
 
Old 12-03-2018, 10:20 AM   #10
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
Root's desktop isn't a very sensible place for a script.
 
Old 12-03-2018, 10:33 AM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,348

Rep: Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992
Quote:
Originally Posted by TenTenths View Post
Also feel free to post your script here using the CODE tags
+1 for this advice...it's pretty difficult to help you with a script you haven't shown us.

I'll also repeat what's already been said a couple of times:
You must either supply a path within the script or use absolute paths to every command in the script in order for it to work via cron
 
Old 12-03-2018, 08:48 PM   #12
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,545

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
Quote:
Originally Posted by atulkvtiwari View Post
Hi i have to add two routes when system reboots or this routes disappear first line is #!/bin/sh and second line this route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X it executes manually running atul.sh but in crontab its not executing so kindly help me
We tried. You seem to have either ignored or misunderstood every bit of advice. Please try again.
 
Old 12-03-2018, 11:50 PM   #13
atulkvtiwari
LQ Newbie
 
Registered: Dec 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
I'll take a guess that your script is in bash, if that's the case add a line like this:
Code:
#!/bin/bash
set -x
then at the end of your cron entry add
Code:
>> /root/Desktop/myscript.log
check /var/log/cron to ensure your job runs and then check the output in the myscript.log file for any clues.

Also feel free to post your script here using the CODE tags


hi i have tried your step but still myscript.log is empty
Code:
#!/bin/bash
set -x
then at the end of your cron entry add
Code:
>> /root/Desktop/myscript.log
so any more suggestion
 
Old 12-03-2018, 11:58 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,818

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
As requested above, post your entire script for us to see...
 
Old 12-04-2018, 12:17 AM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,348

Rep: Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992Reputation: 1992
Quote:
Originally Posted by atulkvtiwari View Post
Hi i have to add two routes when system reboots or this routes disappear first line is #!/bin/sh and second line this route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X it executes manually running atul.sh but in crontab its not executing so kindly help me
Sheesh!
Does that mean
Code:
#!/bin/sh
route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X
What we're all saying to you is to make the second line
Code:
/full/pathto/route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X
on my system that would be
Code:
/sbin/route add -net 0.0.0.0 netmask 0.0.0.0 gw X.X.X.X dev ethX.X
use which route to find out what the full path is on your system

(gonna yell now...)
Add the full path to all commands within the script. There is no PATH in crontab. cron doesn't know where anything is!!
That's the eighth time we've said that in this thread!!

(end yelling)

Last edited by scasey; 12-04-2018 at 12:23 AM.
 
  


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
Error executing shell script by crontab mijan1373 Linux - Newbie 5 07-05-2017 03:06 PM
[SOLVED] crontab not executing the shell script. newhorizons009 Linux - Software 7 12-20-2016 12:31 PM
[SOLVED] Crontab not executing shell when date is added Jakkie Linux - Newbie 14 02-15-2012 06:18 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
Seems crontab is not executing script... jonaskellens Linux - Newbie 3 11-18-2010 09:20 AM

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

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