CRON shell script only runs correctly on command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CRON shell script only runs correctly on command line
# 1  
Old 12-30-2010
Network CRON shell script only runs correctly on command line

Hi, I'm new to these forums, and I'm hoping that someone can solve this problem...

To make things short:

I have DD-wrt set up on a router.

I'm trying to run a script in CRON that fetches the daily password from my database using SSH.

CRON is set like so(in web interface):
Code:
* * * * * root /tmp/root/password-query >/tmp/root/log 2>&1

(I set it to run every minute so I could constantly test it)

And the script goes something like this:
Code:
#!/bin/sh 
export SQL_QUERY="SELECT code FROM wireless WHERE id=1;" 
export REMOTE_COMMAND="mysql -u login --password=xxxxxxxx squid -e '$SQL_QUERY' | tail -c 11" 
export DROPBEAR_PASSWORD="xxxxxxxx" 
export CODE=`/usr/bin/ssh user@xx.xx.xx.xx "$REMOTE_COMMAND"` 
echo "Done" 
echo $CODE 
nvram set wl0_wpa_psk=$CODE 
nvram commit

And run from the command line it DOES SET THE CODE, because it outputs to the log file:
"Done
xxxxxxxx
"

When cron DOES run, the code in nvram is blank and the log file looks like this:
"Done

"

If I change the backticks to quotes and output $CODE, it's exactly the what I want. I have been spending about 8 hours trying to figure this out. What is happening and how can I fix it?

Reward to whoever can suggest a fix: status as a diety
Thanks so much in advance

What I do know:
> The script is executable
> Cron is running and it does run the script every minute and shown by the log file output
# 2  
Old 12-30-2010
The error message(s) should be found in unix mail for the user owning the cron.

Quick tip.
Try a one-off cron containing only a unix "env" command redirected to a logfile. This will tell you how limited the environment for cron is.

What I expect you to find is that the value of $PATH when running from cron does not include the directories containing "mysql" and "nvram".
Also any specific environment variables which are set in your ".profile" will not be set unless you set them in your script.

Best guess. Absolute pathnames for "mysql" and "nvram" and set any relevant environment variables in your script.


Addendum:
Quote:
CRON is set like so(in web interface):

* * * * * root /tmp/root/password-query >/tmp/root/log 2>&1
If this is the actual line in a crontab it is invalid. The sixth field should not be "root" (the word should not be there at all). However if your "web interface" (whatever that is) does not use crontab format please disregard this commment.

Last edited by methyl; 12-30-2010 at 06:59 PM..
# 3  
Old 12-30-2010
Well, because of the firmware, though it is unix based, it requires that you specify a user in the cron file.
Check it out here: http://www.dd-wrt.com/wiki/index.php/CRON (Darn thing won't let me post real URLs yet)

That been my holy book for this project.

I think you might be right about the PATH thing. (I also have a theory that something with the quotes is messing it up) Whatever it is, I realized ssh doesn't output when run from cron. With that being said:

I actually managed to get some sort of workaround that does just fine.

- I made a script on the remote machine that does the query and then outputs the code to a file named 'codelog'
- The script on the router runs, sshes, and executes the script on the remote machine
- Then, the script runs 'scp' to copy the 'codelog' file from the remote machine to the router
- Finally, I set the output of 'cat codelog' as my variable.
- And well, it works!


Thanks so much!
# 4  
Old 12-30-2010
One thing to remember about cron is that it runs with almost no environment, so your script may not know where mysql or even tail reside. It does when you run from the command line as $PATH has been set. You can source a profile explicitly or use absolute paths to your executables to make sure that's not causing your problems.
Hope that helps.

Jerry
# 5  
Old 04-06-2011
MySQL

I hit same problem and found source of the problem after playing around. It appears that SSH starts remote command asynchronously when is started from cron. Don't ask me reason for that I really don't know Smilie My workaround for this in the script below.

Code:
ssh -i "/.ssh/id_rsa" -y root@host.domain.lan "script.sh 2>&1 > /tmp/script.log" 2>&1
# Now we will scp log file from remote host and look for string "Done" in it because ssh executes
# asynchronously when run from cron
rm -f /tmp/ssh.log
timeout=60  # 1 minute time-out
elapsed=0
RC=1

while [ $elapsed -lt $timeout ] && [ $RC -eq 1 ]; do   # We should wait for limited time so that it would not loop forever
   scp -i "/.ssh/id_rsa" root@host.domain.lan:/tmp/script.log /tmp/ssh.log > /dev/null 2>&1

   grep -i "Done" /tmp/ssh.log > /dev/null
   if [ $? -eq 0 ]; then
      RC=0
   else
     sleep 5s
     elapsed=$(($elapsed + 5))
   fi
done
if  [ $elapsed -ge $timeout ]; then
   echo "script failed (time-out after $elapsed seconds)"
fi
Messages=`cat /tmp/ssh.log`
echo "${Messages}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script runs manually, but not from cron

Hi, I "borrowed" a script I found online, to start a SAP router application on a Solaris 11 (SPARC) server. The script runs fine when calling it manually, but when I schedule it to run from cron, it doesn't. I don't see any warning or failure messages anywhere, just nothing happens. ... (11 Replies)
Discussion started by: bredman
11 Replies

2. Shell Programming and Scripting

Sql command inside shell script runs without giving anything back as outout

#!/bin/sh # This script returns the number of rows updated from a function echo "The execution is starting ....." sqlplus -silent $UP <<EOF set serveroutput on set echo off set pagesize 0 VAR no_rows_updated NUMBER; EXEC :no_rows_updated :=0; DECLARE CURSOR c_update is SELECT * FROM... (4 Replies)
Discussion started by: LoneRanger
4 Replies

3. Shell Programming and Scripting

Script runs in command-line fine but times out in CRON?

Hi, I have a script that seems to run to completion when in the command-line, but when it is run using the cron, it seems to time out. They both start and run fine, but on the CRON it stops prematurely. The script hits an API every few seconds and grabs data. Does anyone have any idea on... (4 Replies)
Discussion started by: phpchick
4 Replies

4. Shell Programming and Scripting

Odd results when my script runs from cron..

Hi folks, So I wrote a script to run "top", "awk" out values fro the "top" and send the results to a data file. I then set it to run in cron every 15 minutes. Now I'm noticing that the script, and it's sub-commands are not always cleanly finishing and, in my investigations, I am also... (11 Replies)
Discussion started by: Marc G
11 Replies

5. AIX

Script runs in shell but not cron

We run some menu driven software that has the ability to batch menu paths and generate reports quickly. Normally you run a batch like: $ BATCH BATCHNAME The batch program then prompts you for the date you want the report run for. I got some help from some folks on IRC to do the following: BATCH... (2 Replies)
Discussion started by: herot
2 Replies

6. Shell Programming and Scripting

Shell Script runs good manually but not through Cron tab

Hello Every one, I have a shell script which is running fine manually, but its giving me hard time when running tru cron job. :wall:. Am using #!/usr/bin/ksh >echo $SHELL /usr/bin/ksh Cron Job is as below, it execues but dosent do what i want it to do. 47 15 * * *... (1 Reply)
Discussion started by: naren.chowdhary
1 Replies

7. Shell Programming and Scripting

Script runs manually but not correctly from crontab

Hello all, I'm new here and have a question if you don't mind helping me. I have a script that will work if I kick if off manually but not from Cron. My cron entry is this: 05,20,35,50 * * * * /scripts/status.sh > /dev/null 2>&1 The first script (works fine) is this: #!/bin/sh # #... (14 Replies)
Discussion started by: hs3082
14 Replies

8. Shell Programming and Scripting

Script runs manually but not correctly from crontab

Hi all I have this inside a shell script (bash): cd DIRECTORY find . -maxdepth 1 | sed 's#./##' | /usr/bin/xargs -I '{}' chown -Rv '{}' /DIRECTORY/'{}' All the directories in this location are named after usernames, so it simply sets the owner to that of the username of the folder. It... (5 Replies)
Discussion started by: fakesy
5 Replies

9. Shell Programming and Scripting

Shell script runs fine in Solaris, in Linux hangs at wait command

HI, I have a strange problem. A shell script that runs fine on solaris. when i ported to linux, it started hanging. here is the core of the script CFG_FILE=tab25.cfg sort -t "!" -k 2 ${CFG_FILE} | egrep -v "^#|^$" | while IFS="!" read a b c do #echo "jobs output" #jobs #echo "jobs... (13 Replies)
Discussion started by: aksaravanan
13 Replies

10. Shell Programming and Scripting

Script runs fine, but not in a cron

Okay, I have the following script that runs fine from a command line as well as an executable .sh file. It just moves any file/folder with movie* in the name to a folder called _Movies. The issue I'm running into is when it's call from a cron. find /mnt/HD_a2/BT/complete -iname "movie.*" -exec... (4 Replies)
Discussion started by: sammyk
4 Replies
Login or Register to Ask a Question