Job runs manually, doesn't work in crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Job runs manually, doesn't work in crontab
# 8  
Old 06-13-2016
Without you showing us the contents of /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export (if it is a shell script) or the source code that was used to produce /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export (if it is a binary executable) we have absolutely no way to guess at what the argument to it means. It that parameter is used to create an output file, maybe /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export did create that file for you somewhere (in a directory you haven't searched where cron ran your job). If that parameter names an input file, you absolutely either need to be sure that your script changes its current working directory to that directory before it starts /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export or you need to provide the name of that file as an absolute pathname.

The common thing about the hundreds of threads started in this forum about a job that works when run on the submitter's shell command line works, but doesn't work when run from cron is that cron does not store your current shell execution environment in crontab and setup that same shell execution environment for the job when it is run. YOU have to make sure that any script you run from cron creates a shell execution environment that uses the proper shell to run your cron job, positions itself in a directory where your script has access to input and output files it needs, and has environment variables set appropriately for any variables the processes started by your script need to operate properly.

Your request is essentially saying my shell script doesn't work. Fix it for me. And, without any details about what your script does, you KNOW that we can't possibly do that other than to make the generic suggestions we have already given you.
# 9  
Old 06-13-2016
Thx again for the response. I don't know how to show the source code for the executable. How do I do that?

I do know that the job is NOT creating the output file anywhere.

"Your request is essentially saying my shell script doesn't work. Fix it for me." - That's not what I'm saying (or not my intent). The script does work. It runs the upgrade_export executable that creates an output file -
Code:
/opt/CPsuite-R77/fw1/bin/upgrade_tools/mgt-svr-bkup-$today.tgz

So I'm not asking to fix the script, I'm wondering why it runs manually and not in crontab. If the reason is I need to set the correct shell execution environment, then at least I have something to work on. But my thought in posting this was maybe there was something obvious I was missing that was the problem. I'm new at this and trying to figure it out.
# 10  
Old 06-14-2016
If you want a simple fix - start with environment variables.

There are two commands: set and env. set is a shell builtin and shows all variables including those visible locally (not exported). env command cannot do that, it sees only those environment variables the process got at process creation. env is a separate physical file not a builtin. So you want to use env to really see what is going on -in your case not going on.


One crude way:
create a one line script called script.shl:
Code:
/usr/bin/env

run it two ways -- (redirect the output of each run to different file names. For crontab /tmp is a handy place) manually, then in crontab

The diff command will see what crontab does to your login variables. Add 'em to your script that runs the problem executable - be sure to export the variables.
# 11  
Old 06-14-2016
To post code use code tags -- these are examples with embedded spaces; html does not interpret them
[ c o d e ]
script text goes here
or FORTRAN, C, C++ ,etc. code goes here between a separate set of code tags for each program file.
[ / c o d e ]

Obviously, remove the spaces from the example to make it work.
Code:
script text goes here
or FORTRAN, C, C++ ,etc. code goes here between a separate set of code tags for each program file.

# 12  
Old 06-14-2016
Look at the threads at the bottom of this page. There are five threads there with exactly the same problem you are having and the same suggestions we have been making here solved the problem for all of them.

When you run the program manually and it works, what shell are you using? Change the 1st line of your script from:
Code:
shell=/bin/bash

to:
Code:
#!pathname

where pathname is the absolute pathname of the shell you are using. Does that change make your script work when run by cron?

If not, when you run the program manually and it works, what directory is your current working directory? What is the output from the command:
Code:
pwd -P

? Add a command to your script before you invoke /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export that changes your current working directory in your script to that directory:
Code:
cd directory

where directory is the string returned by the above pwd command. After that change, does your script work when run by cron?

When you run the program manually and it works, what environment variables does your program need to run correctly? Does your program work correctly if you invoke it manually with the commands:
Code:
today=$(date +"%m-%d-%y")
env - PATH="/bin:/usr/bin" /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export mgt-svr-bkup-$today << EOF
y
n
EOF

? If not, what environment variables does /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export need to run correctly? Find out (using the methods suggested by Jim McNamara) and set those environment variables in your script before you invoke /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export.
# 13  
Old 06-14-2016
Thanks, Jim!
I created the script /usr/bin/env and ran it manually and in crontab.

The PATH variable in the manually-run output file is long. The PATH variable in the file run in crontab is short - PATH=/usr/bin:/bin. So, thinking I needed to tell cron to use the long PATH variable, I added it into the script: (PATH=/opt/CPsuite-R77/...), but that didn't work when I ran the job from crontab.

Thanks for the response, Don.
"Look at the threads at the bottom of this page. There are five threads there with exactly the same problem you are having and the same suggestions we have been making here solved the problem for all of them."

If that were true, I'd have figured it out by now. The problems in the other threads are "similar", but not "exactly the same". I read through them before I posted my issue. Had I been able to solve my problem using suggestions posted in the other threads, I wouldn't have posted mine.

I get that my problem probably has to do with setting the environment variables, but it's not clear to me yet how to do that.

"When you run the program manually and it works, what shell are you using? Change the 1st line of your script from:

Code:
shell=/bin/bash

to:

Code:
#!pathname

where pathname is the absolute pathname of the shell you are using. Does that change make your script work when run by cron ?"

Since the first line in the script is shell=bin/bash, I'm using the bash shell, right? Changed the first line to #!/bin/bash. Still doesn't work using cron.

I'm running the job from my home directory -

Code:
/home/admin

"? Add a command to your script before you invoke
Code:
/opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export

that changes your current working directory in your script to that directory:

Code:
cd directory

where directory is the string returned by the above pwd command. After that change, does your script work when run by cron ?"

Still doesn't work.

"When you run the program manually and it works, what environment variables does your program need to run correctly? Does your program work correctly if you invoke it manually with the commands:"


Code:
today=$(date +"%m-%d-%y")
env - PATH="/bin:/usr/bin" /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export mgt-svr-bkup-$today << EOF
y
n
EOF

Yes.
# 14  
Old 06-14-2016
Quote:
Originally Posted by df08388
... ... ...
I get that my problem probably has to do with setting the environment variables, but it's not clear to me yet how to do that.

"When you run the program manually and it works, what shell are you using? Change the 1st line of your script from:

Code:
shell=/bin/bash

to:

Code:
#!pathname

where pathname is the absolute pathname of the shell you are using. Does that change make your script work when run by cron ?"

Since the first line in the script is shell=bin/bash, I'm using the bash shell, right?
Yes, No, Maybe!
If the default shell for your system is /bin/bash, then having the line:
Code:
shell=/bin/bash

in your script does nothing and your script is being run by bash. If the default shell for your system is NOT /bin/bash, then having the line:
Code:
shell=/bin/bash

in your script does nothing and your script is NOT being run by bash; it is being run by the default shell on your system.

If you type the command:
Code:
shell=/dev/null

into your shell, does the shell you are using magically change to /dev/null? NO. All it does is set a shell variable that later lines in your script (or, when done interactively, in your login session) can access by testing the value of $shell.

By convention (when your login shell is not a csh derivative), when you login the shell variable SHELL is exported and set by the system to an absolute pathname of your login shell. My login shell is /bin/ksh and after I login, $SHELL expands to /bin/ksh. If I then invoke bash, $SHELL still expands to /bin/ksh. Note also that case matters: $SHELL and $shell are two different variables.

If the script you are asking cron to run is an executable file and the 1st two characters in that file are #!, the system will use the interpreter named by the pathname on the rest of that line as the program used to interpret the rest of that file.

Now, back to your environment problem...
If you don't know what environment variables /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export needs to have set in order for it to work correctly, you need to set all of the variables in your script to the same values that they have when you run /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export manually and it works.

When you login with bash as your login shell, it runs three files from your home directory if they are present: .bash_profile, .bash_login, and .profile. If those files are where the variables /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export needs to run are defined, the following might work for you. Change:
Code:
shell=/bin/bash

in your script to:
Code:
#!/bin/bash
HOME="/absolute/path/to/your/home/directory"
cd "$HOME"
[ -f .bash_profile ] && . .bash_profile
[ -f .bash_login ] && . .bash_login
[ -f .profile ] && . .profile

One would expect that one or more of those three files initializes PATH (although it appears that your script doesn't depend on any utilities that are not on the default PATH that you aren't referencing with absolute pathnames) and other shell variables to the values you'll need to run /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export, but we can't know that for sure since we have no idea what other commands you have run after you last logged in to your system before you successfully run /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export from your interactive shell. And, as we have repeatedly said, without knowing what /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export is doing, we are just guessing. If /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export tries to open your controlling terminal for some reason, it might just not be possible to run /opt/CPsuite-R77/fw1/bin/upgrade_tools/upgrade_export from cron at all.
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. UNIX for Dummies Questions & Answers

Script runs manually but not from crontab in UNIX

Hi Guys, I am executing the script called Delet.sh manually it is successfully completing the task but it is failing to run vi cron tab, I tried to pass PATH & .profile before execution but no luck, Any suggestions? Script below #!/usr/bin/ksh #set -x # Purpose : Delete folders file from... (9 Replies)
Discussion started by: Anilsa77
9 Replies

3. Shell Programming and Scripting

Script runs good manually but failing via crontab

Hello Gurus, I have written small script which will start the given service if its stop .Its running fine when manually executed but its unable to run from crontab. #!/bin/bash SERVICENAME=rsyslog service $SERVICENAME status > /dev/null SYSLOGSTATUS=`echo $?` COUNT=0 THRESHOLD=3 if ... (4 Replies)
Discussion started by: kapil514
4 Replies

4. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

5. Shell Programming and Scripting

Script runs fine manually but not in crontab

Hello Guys, I have scratched my head alot on this but couldn't find clue what's wrong. Can you please help me with this? My problem is as following. 1) When I manually execute following script it runs successfully with below output. bash-3.00# more smssend #!/bin/bash echo -e "<Request... (16 Replies)
Discussion started by: umarsatti
16 Replies

6. Shell Programming and Scripting

cronjob not running but runs manually

hello, i recently switched to a new ec2 box and transferred the cronjobs from the old box. For some reason one of the scripts won't work but it runs manually from the command line. I've read from previous threads it might be an environment issue but I added a line with the path to the script... (2 Replies)
Discussion started by: lencholamas
2 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

IDL job doesn't work from crontab

I have made a script to execute an IDL routine with the purpose to plot data on a fixed time. The problem is that when I include this script in the crontab to run it every night, the IDL part doesn't work (the other commands, like getting data from the database, are carried out though). This... (4 Replies)
Discussion started by: SharkM
4 Replies

10. Shell Programming and Scripting

Expect script doesn't work under crontab

Hi All, Using Expect script when I run it manually it works. But when I put the entry in crontab, the job is still running after 15 hours. The script was created as root. I don't think it's a permission issue. Any idea? This is what I have under root crontab... 00 18 * * 1-5... (4 Replies)
Discussion started by: samnyc
4 Replies
Login or Register to Ask a Question