Linux/bash Script only working if executed from shell prompt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux/bash Script only working if executed from shell prompt
# 1  
Old 04-25-2016
Linux/bash Script only working if executed from shell prompt

Hi,

maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt?

Code:
#! /bin/bash

set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

date >> /var/log/cronlog
ping -c 3 10.0.0.1 2>>/var/log/cronlog 1>/dev/null || (service ipsec restart >>/var/log/cronlog && exit 0)
ping -c 3 172.16.1.1 2>>/var/log/cronlog 1>/dev/null || service ipsec restart >>/var/log/cronlog

The purpose of the script is self-explaining. It pings two remote vpn gateways and restarts ipsec, if one of the pings fails. Timestamps, errors and ipsec restart are logged.

Please, help!

b.
# 2  
Old 04-25-2016
The command:
Code:
set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

is something that you would use in a csh script; not bash. In a bash script, that command sets the 1st positional parameter for the current shell execution environment to PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin; it does not change the value of the PATH environment variable for that script. Try changing it to:
Code:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

If that doesn't solve your problem, please answer the following questions:
  1. Who are you logged in as when the script succeeds when you run it from your shell?
  2. Whose crontab is the script run from when cron runs the job? What is the entry in that crontab that starts this script?
  3. What is the output from the command?: ls -ld /var/log /var/log/cronlog
  4. What is the output from the command?: type date ping service

Last edited by Don Cragun; 04-25-2016 at 09:39 PM.. Reason: Explain what set PATH=... does in bash.
# 3  
Old 04-26-2016
Thanks a lot!!!

Three lousy letters! Removed the 'set' and it worked.. ..

Smilie
# 4  
Old 05-12-2016
Quote:
Originally Posted by Don Cragun
The command:
Code:
set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

is something that you would use in a csh script; not bash. In a bash script, that command sets the 1st positional parameter for the current shell execution environment to ]
Found it in the google
Thank you! Have exactly the same problem there!
This User Gave Thanks to imort For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh. more script1.sh echo "Starting Execution" ./script2.sh echo "End of Execution" ln_s(){ ] && return ln -s "$1" "$2" } My script1.sh executes another script2.sh which has the following entry more script2.sh... (12 Replies)
Discussion started by: mohtashims
12 Replies

2. UNIX for Dummies Questions & Answers

Shell script not working but command works in command prompt

Hi everyone I have a problem with my script If I try directly this command /usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz It works But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies

3. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

Hi All, I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help? Sample file: TS3*1451575*12*20151231*4*482.44 NM1*QC*1*CUTLER*BETTY DTM*472*20150808... (4 Replies)
Discussion started by: Venkata Prasad
4 Replies

4. UNIX for Dummies Questions & Answers

Bash script dont works when executed as cronjob

Hello, i have cronjob: crontab -l * * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh it runs: /var/log/cron Mar 25 12:11:01 vps crond: (root) CMD (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh) but somehow script dont run properly via cronjob. But when i execute cronjob... (7 Replies)
Discussion started by: postcd
7 Replies

5. Homework & Coursework Questions

LINUX Bash Shell Script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a bash shell script that presents work information of employees of a department from a company data... (1 Reply)
Discussion started by: help123
1 Replies

6. Shell Programming and Scripting

PLEASE HELP! LINUX BASH SHELL SCRIPT

PLEASE HELP! NEED LINUX SCTIPT Need to write a bash shell script to show information of employees of a department from a company data set. The script should accept a project number (1/2/3/10/20/30) and output * the name of the project * the name of the manager of the controlling... (1 Reply)
Discussion started by: help123
1 Replies

7. UNIX for Advanced & Expert Users

Script working successfully only when executed twice

Guys, i am facing a very strange issue, my code below does an ftp to server A and gets a file to Server B, once the file is in B an if condition is present to check if the pattern of the filename is ABC* then it has to be encrypted using OPENSSL as ABC.enc else if it of pattern 123* has to be... (3 Replies)
Discussion started by: meva
3 Replies

8. Shell Programming and Scripting

returning un executed string to shell prompt

Hi all, i'm pritty new to chell scripting I'm trying to find a way to return a value to shell without it executing. is there a special character that will encase a sting including the command to a shell without executing, so waiting for the user to press enter. say i wanted to return a value... (3 Replies)
Discussion started by: jvan
3 Replies

9. Linux

Local shell script need to be executed on a remote linux box

I need to execute a shell script on a remote linux box. But the shell script resides on the local linux box where I am currently logged in. Is there a way to do this? I know rsh <host> <command> can execute a command on the remote host. (6 Replies)
Discussion started by: rajeshomallur
6 Replies

10. Shell Programming and Scripting

how to make a bash script that can be executed by people simultaneously?

dear friends, i want to make a bash script that can be executed by many people simultaneously. do you have any idea to make it? there will be many dependent-variables(which is input from people) in the scripts. i am thinking about a random temporary file that created by the bash script each... (4 Replies)
Discussion started by: jimmbp
4 Replies
Login or Register to Ask a Question