Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running script in cron vs cmd line

Status
Not open for further replies.

westwood01

Technical User
Dec 28, 2003
41
US
When I run the below script from the cmd line it works as expected. The script will accurately detect whether an ssh connection is available. But, when I put this same script in cron it fails every time, even when an ssh connection is available. Any ideas? Or possibly a better method to monitor ssh connectivity? Thanks guys.

#!/bin/ksh
#set -x
ssh somehost 2>/dev/null &
sleep 5
kill $!
if [ $? -eq 0 ];then
echo $? > /export/home/oper/host.exit
echo successful >> /export/home/oper/host.exit
else
echo $? > /export/home/oper/host.exit
echo failed >> /export/home/oper/host.exit
cat /export/home/oper/host.exit|mailx -r me@mywork.com -s "somehost failed ssh connection" me@mywork.com
fi
 
depends on where ssh is.

If ssh is located, for example, under /usr/local/bin, then you will need to either source root's profile in the script or use absolute path names in the the script for the command.

Regards,
Chuck
 
The environment used by cron is different from the environment used when you're running a command line. In particular .profile isn't run. This can be fixed in a number of ways but the simplest is to make the command
Code:
su - root - c /path/to/myscript

Ceci n'est pas une signature
Columb Healy
 
I find this much easier.

if nmap -p 22 wo9|grep "22/tcp open"
then echo "ok"
else echo "failed"
fi



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I tried using the first two suggestions:
specifying full ssh path
adding su - root -c /path/to/myscript to cron

Script still fails.

I'll have to install nmap to try the nmap option, since I dont have it by default w/ solaris10.
 
Try

1. Reinstate the 'set -x' or make the first line '#!/bin/ksh -x'
2. Amend the cron entry to
Code:
su - root -c "/path/to/myscript > /tmp/mylog 2>&1"

And post the output.

Ceci n'est pas une signature
Columb Healy
 
# more mylog
kill: 16760: no such process

I'm now thinking that the script is returning a failed run code, since the kill $! is failing to find a process. Is this more a problem with the writing of the script as opposed to running it out of cron?
 
Hmm, I'm not sure that ssh can work in this way - which is, after all, your original point.

Howabout

Code:
#!/bin/ksh

ssh somehost ls >/dev/null 2>&1 && RES=success || RES=failure
echo ssh to somehost returned $RES | mail -s "ssh on somehost" me@myhost
Of course this doesn't e-mail the result code but I don't think that's an issue.


Ceci n'est pas une signature
Columb Healy
 
thanks guys, I ended up going a different route. Instead of trying to initiate an ssh or telnet session in the script I just did an rsh. This ultimately gave me what I was looking for, since the box I was testing was oddly rebooting into single user mode, so if my rsh (telnet or ssh) failed I got what I was looking for. thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top