473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Cron Job Not Working

Hi,

I am trying to run a simple shell script which executes asl in return. I am able to execute the shell scrip manually but i am not able to run the script through cron job. Can anybody help me out? Is it something to do with environment variables?My script is

#!/bin/sh
cd /opt/InCh/IP/smarts/bin
sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log

exit 0

Cheers,
Amit
Jul 28 '07 #1
10 11071
numberwhun
3,509 Expert Mod 2GB
Hi,

I am trying to run a simple shell script which executes asl in return. I am able to execute the shell scrip manually but i am not able to run the script through cron job. Can anybody help me out? Is it something to do with environment variables?My script is

#!/bin/sh
cd /opt/InCh/IP/smarts/bin
sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log

exit 0

Cheers,
Amit
I can see that you did a "cd" to the directory, but instead, run the sm_adapter command with the full path before it and see if it works.

Regards,

Jeff
Jul 29 '07 #2
I can see that you did a "cd" to the directory, but instead, run the sm_adapter command with the full path before it and see if it works.

Regards,

Jeff
Hi Jeff,

Thanks for the suggestions. Yes, i tried that way also but it is not working. In frustation, i serached through the internet and found that many people face this problem. This is something to do with environment variable -- shell takes different set of environment variables and crontab takes different. But i could not understand how i can modify my script or change the environment variables. If you could through some light from that side, it would be great!!

Cheers,
Amit
Jul 30 '07 #3
numberwhun
3,509 Expert Mod 2GB
Hi Jeff,

Thanks for the suggestions. Yes, i tried that way also but it is not working. In frustation, i serached through the internet and found that many people face this problem. This is something to do with environment variable -- shell takes different set of environment variables and crontab takes different. But i could not understand how i can modify my script or change the environment variables. If you could through some light from that side, it would be great!!

Cheers,
Amit
If I remember right, when the cron job runs, it is actually root that runs the job. That is why the need for absolute paths. As for the environment variables, if one of them is not getting set correctly, maybe you can put a line in the script to change it to what you need it to be.

Regards,

Jeff
Jul 30 '07 #4
prn
254 Expert 100+
A cron job is not necessarily run as root. Any user may have a crontab and the jobs in that crontab are run as that user. However, the crontab job does not necessarily exec the user's .profile (.bash_profile, .cshrc, whatever) so the user's environment is not necessarily the same in the context of the cron job as it is for an interactive job. Jeff's suggestion of adding the full path to the sm_adapter command is the most common solution to the problem, but adding "./" to the beginning of that line should be equivalent since you already did a cd to its directory. Since that did not work, the next thing to try is to actually find out what is going wrong. :-/

You need to catch the output from your cron job (not just the output from the sm_adapter command) so make sure that your crontab entry redirects both STDOUT and STDERR to a file. How you do that will depend on your shell, but most likely, the entry in your crontab file should look something like:
Expand|Select|Wrap|Line Numbers
  1. 30 23 * * *  /home/whatever/foo >/home/whatever/mylog 2>&1
and now the file "/home/whatever/mylog" should contain relevant information.

Since the most likely cause for your problem is your environment settings, I would suggest adding a line in your cron job to catch that too, e.g.:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. env
  3. cd /opt/InCh/IP/smarts/bin
  4. ./sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log
  5.  
  6. exit 0
Addition of the "env" line should add a listing of your environmental settings to the /home/whatever/mylog file. Comparing the environment listing with the equivalent from an interactive session may also give you a clue about where your problem lies.

If it comes down to it, you may be able to execute your .profile inside the cron job to get comparable environment variables, but that's not the first thing I would suggest, and I certainly would not suggest it if you are not clear on what works in interactive and non-interactive contexts. Too many people do unsafe things in their .profiles for me to feel comfortable recommending this without knowing a lot more than I do about your situation. More likely there are relatively specific things you should add to your cron job to set the environment. I'll refrain from suggesting any of those until I know more about your situation.

HTH,
Paul
Jul 30 '07 #5
arne
315 Expert 100+
Hi,

I am trying to run a simple shell script which executes asl in return. I am able to execute the shell scrip manually but i am not able to run the script through cron job. Can anybody help me out? Is it something to do with environment variables?My script is

#!/bin/sh
cd /opt/InCh/IP/smarts/bin
sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log

exit 0

Cheers,
Amit
Are you sure your your cron job is invoked at all? I once had a problem that the cron job was not invoked after I copied the crontab from one server to another. The reason was a missing newline at the the very end of the crontab ...

arne
Aug 1 '07 #6
A cron job is not necessarily run as root. Any user may have a crontab and the jobs in that crontab are run as that user. However, the crontab job does not necessarily exec the user's .profile (.bash_profile, .cshrc, whatever) so the user's environment is not necessarily the same in the context of the cron job as it is for an interactive job. Jeff's suggestion of adding the full path to the sm_adapter command is the most common solution to the problem, but adding "./" to the beginning of that line should be equivalent since you already did a cd to its directory. Since that did not work, the next thing to try is to actually find out what is going wrong. :-/

You need to catch the output from your cron job (not just the output from the sm_adapter command) so make sure that your crontab entry redirects both STDOUT and STDERR to a file. How you do that will depend on your shell, but most likely, the entry in your crontab file should look something like:
Expand|Select|Wrap|Line Numbers
  1. 30 23 * * *  /home/whatever/foo >/home/whatever/mylog 2>&1
and now the file "/home/whatever/mylog" should contain relevant information.

Since the most likely cause for your problem is your environment settings, I would suggest adding a line in your cron job to catch that too, e.g.:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. env
  3. cd /opt/InCh/IP/smarts/bin
  4. ./sm_adapter -s APM1 /opt/InCh/IP/smarts/local/rules/Cards.asl >> /opt/InCharge65/IP/smarts/bin/xvz.log
  5.  
  6. exit 0
Addition of the "env" line should add a listing of your environmental settings to the /home/whatever/mylog file. Comparing the environment listing with the equivalent from an interactive session may also give you a clue about where your problem lies.

If it comes down to it, you may be able to execute your .profile inside the cron job to get comparable environment variables, but that's not the first thing I would suggest, and I certainly would not suggest it if you are not clear on what works in interactive and non-interactive contexts. Too many people do unsafe things in their .profiles for me to feel comfortable recommending this without knowing a lot more than I do about your situation. More likely there are relatively specific things you should add to your cron job to set the environment. I'll refrain from suggesting any of those until I know more about your situation.

HTH,
Paul
Thanks Paul for such a nice expalnation. I will try it it.

Cheers,
Amit
Aug 1 '07 #7
Are you sure your your cron job is invoked at all? I once had a problem that the cron job was not invoked after I copied the crontab from one server to another. The reason was a missing newline at the the very end of the crontab ...

arne
Hi Arne,

There are some other utilities which are running through that crontab. So i think that the crontab is running. I will try to debug the problem as suggested by Paul earlier.

Thanks a lot for your inputs

Cheers,
Amit
Aug 1 '07 #8
arne
315 Expert 100+
Hi Arne,

There are some other utilities which are running through that crontab. So i think that the crontab is running. I will try to debug the problem as suggested by Paul earlier.

Thanks a lot for your inputs

Cheers,
Amit
Still: even if other entries work, it doesn't mean that cron is able to parse the last entry ... this was the problem I had. All other entries worked just fine and there was no message about a corrupted entry or something similar. cron just silently didn't execute the last line as it was not ending in a newline.

arne
Aug 1 '07 #9
Still: even if other entries work, it doesn't mean that cron is able to parse the last entry ... this was the problem I had. All other entries worked just fine and there was no message about a corrupted entry or something similar. cron just silently didn't execute the last line as it was not ending in a newline.

arne
Is this behaviour just on linux?? Reason i ask is that i am moving some db's from Solaris to Linux RHE4. there is no blank line on the solaris crontab, and the last line works....

cheers
mark
Oct 1 '07 #10
arne
315 Expert 100+
Is this behaviour just on linux?? Reason i ask is that i am moving some db's from Solaris to Linux RHE4. there is no blank line on the solaris crontab, and the last line works....

cheers
mark
I didn't check with Solaris. The copy was between two different versions of Linux. On one machine it worked, on the other it didn't.

arne
Oct 4 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Saurabh Agarwal | last post by:
Hi all, I had scheduled a script using cron and got pid of it when it is running as it is running for a long time.The script is somewhat like cmd sleep 10 cmd2 sleep 20 ... This is...
6
by: sherryonline | last post by:
i have a page and i need that page to be called every day at 12:00 that page is going to connect to the database fetch some emails and will start mailing them how i am goning to do this.
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
3
by: the.natalie | last post by:
Hi. I am a newbie to mysql, cron, and shell scripting, so please bear with me. I have a script that is used for updating an image directory based on contents in a database. The script does the...
3
by: dburdick | last post by:
I was referred to this forum as this may be a php specific problem. Some kind of configuration problem maybe. I have some php files that connect to a mysql database and are run as cron jobs. ...
4
by: pushpen | last post by:
I'm trying to run the following php code along with gd library as a cron job ,but cron is not generating any output. When I run this code from my browser I get an image correctly. Somebody please...
4
by: Shelly | last post by:
I am coding a psudo-cron job into an application that I am writing. I don't have access to write a real cron job. Here is what I did: 1 - I downloaded pseudocron.php from its website...
1
by: symbioid | last post by:
Hello, I'm working on a project, and VMware has problems with suspending the virtual machine. We are accessing the machine through samba. However, when I suspend the VM, it stops the Samba...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
1
by: Karthik Gurusamy | last post by:
Hi, I'm working on a cron like functionality for my application. The outer loops runs continuously waking every x seconds (say x=180, 300, ..). It needs to know what events in cron has expired...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.