?
Solved

shellscript won't work as cronjob

Posted on 2003-03-07
7
Medium Priority
?
362 Views
Last Modified: 2010-04-20
Hi!

Problem is:
I want to shutdown a server when all Clients in the network are unreachable using ping.

I wrote this script (pardon me, I'm a beginner with shellscripts in linux... :-). There might be a more clever way do do this... )

To explain it shortly: There are two clients in the net at the moment. Im writing the ping-results into a file, and filter using tail/head/cut the number of received packets. When all packets at all clients are lost, the job initiates a shutdown.

Here's the script:

#!/bin/bash
ping 192.168.0.13 -w 1 > /var/log/ping_13.txt
tail -2 /var/log/ping_13.txt | head -1 > /var/log/ping_13.txt
cut -c24 /var/log/ping_13.txt > /var/log/client_dead_13.txt
ping 192.168.0.14 -w 1 > /var/log/ping_14.txt
tail -2 /var/log/ping_14.txt | head -1 > /var/log/ping_14.txt
cut -c24 /var/log/ping_14.txt > /var/log/client_dead_14.txt
# wenn alle pakete bei allen clients verloren gingen -> Rechner runterfahren
a=$(more /var/log/client_dead_13.txt)
b=$(more /var/log/client_dead_14.txt)
if [ $a -eq 0 -a $b -eq 0 ]
then
echo "$(date) - Clients down" >> /var/log/ping_shutdown.log
/sbin/shutdown -h now
else
echo "$(date) - Clients alive : $a $b" >> /var/log/ping_shutdown.log
fi
#EOF

This script works fine. No errormessage. But only, if this sript is startet from a prompt by root.
When I put this into /etc/cron.hourly ,the script works every hour, but generates an erromessage:

From root@BASEMENT Thu Mar 6 19:02:03 2003
Return-Path: <root@BASEMENT>
Received: from BASEMENT (localhost.localdomain [127.0.0.1])
by BASEMENT (8.12.5/8.12.5) with ESMTP id h26I2323001960
for <root@BASEMENT>; Thu, 6 Mar 2003 19:02:03 +0100
Received: (from root@localhost)
by BASEMENT (8.12.5/8.12.5/Submit) id h26I23uw001958
for root; Thu, 6 Mar 2003 19:02:03 +0100
Date: Thu, 6 Mar 2003 19:02:03 +0100
Message-Id: <200303061802.h26I23uw001958@BASEMENT>
From: root@BASEMENT (Cron Daemon)
To: root@BASEMENT
Subject: Cron <root@basement> run-parts /etc/cron.hourly
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/>
X-Cron-Env: <LOGNAME=root>

/etc/cron.hourly/ping_shutdown.sh:

/etc/cron.hourly/ping_shutdown.sh: line 11: [: too many arguments

--

Output in /va/log/ping_shutdown.log is this:
Thu Mar 6 19:01:02 CET 2003 - Clients alive : ::::::::::::::
/var/log/client_dead_13.txt
::::::::::::::
1 ::::::::::::::
/var/log/client_dead_14.txt
::::::::::::::

Output when started as root is:
Mit M&#9500;ñr 5 22:15:18 CET 2003 : Clients alive

Rights are granted.

Anyone an idea why this script won't work as cronjob?

Greetings

Holger
0
Comment
Question by:hhimmel
7 Comments
 
LVL 6

Expert Comment

by:mbarbos
ID: 8087772
try to use the full path for ALL the comands in the script, like "/bin/ping" and not just "ping"
0
 

Expert Comment

by:jonathanrcarter
ID: 8088100
how are you starting the command in your cron.hourly file.

It could be that you should explicitly prefix the script name with the shell interpreter you are using

i.e. /bin/bash /<location>/ping_shutdown.sh
0
 
LVL 20

Expert Comment

by:Gns
ID: 8088309
The error is quite plain:
The bash builting test command (here in the guise of "[" bombs out on the line:
if [ $a -eq 0 -a $b -eq 0 ]

this is because one or both of $a or $b is unset.
This isn't at all surprising either, since the use of more implies a controlling terminal, which you have when you run it from the command line, but utterly lack when you run it from cron.

Replace
a=$(more /var/log/client_dead_13.txt)
b=$(more /var/log/client_dead_14.txt)
with
a=$(cat /var/log/client_dead_13.txt)
b=$(cat /var/log/client_dead_14.txt)
and things would run smoother:-).

-- Glenn
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
Gns earned 300 total points
ID: 8088612
I couldn't help myself...;-)
Slightly improved script, dispensing with the temporary files, but otherwise functionally equvalent:
#!/bin/bash
# wenn alle pakete bei allen clients verloren gingen -> Rechner runterfahren
a=$(ping 192.168.0.13 -w 1 | grep transmitted | cut -c24)
b=$(ping 192.168.0.14 -w 1 | grep transmitted | cut -c24)
if [ $a -eq 0 -a $b -eq 0 ]
then
echo "$(date) - Clients down" >> /var/log/ping_shutdown.log
/sbin/shutdown -h now
else
echo "$(date) - Clients alive : $a $b" >> /var/log/ping_shutdown.log
fi
#EOF

Enjoy
-- Glenn
0
 
LVL 20

Expert Comment

by:Gns
ID: 8088694
Might one also suggest that you carefully read the section "Paramater Expansion" in the bash man-page?
Especially the parts regarding ${param:-value}, ${param:=value}, ${param:?value} and ${param:+value} should be of interrest.

-- Glenn
0
 

Author Comment

by:hhimmel
ID: 8090588
Hi!

Thanks a lot for guidance.

The fine working script looks like this now:

#!/bin/bash
a=$(/bin/ping 192.168.0.13 -w 1 | grep transmitted | cut -c24)
b=$(/bin/ping 192.168.0.14 -w 1 | grep transmitted | cut -c24)
if  [ $a -eq 0 -a $b -eq 0 ]
  then
    echo "$(date) - Clients down" >> /var/log/ping_shutdown.log
    /sbin/shutdown -h now
  else
    echo "$(date) - Clients alive : $a $b" >> /var/log/ping_shutdown.log
fi
#EOF
0
 
LVL 20

Expert Comment

by:Gns
ID: 8093027
Oh, so I earned a B for not indenting?-) Or for recommending that you spend some time with the documentation?-):-)
Not that I really care about points/grades... It just baffled me;-)

-- Glenn
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question