?
Solved

shellscript won't work as cronjob

Posted on 2003-03-07
7
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Industry Leaders: 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!

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

743 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