Odd results when my script runs from cron..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Odd results when my script runs from cron..
# 1  
Old 02-25-2013
Odd results when my script runs from cron..

Hi folks,

So I wrote a script to run "top", "awk" out values fro the "top" and send the results to a data file.

I then set it to run in cron every 15 minutes.

Now I'm noticing that the script, and it's sub-commands are not always cleanly finishing and, in my investigations, I am also noticing that the script is launching differently than expected from cron. Some of this evidence comes from my ps, which shows processes lingering after completing as shown here:
Code:
     UID   PID  PPID   C    STIME TTY         TIME CMD
    root 26793   287   0   Feb 21 ?           0:00 sh -c /usr/apps/client/bin/gatherOSKPI.sh
    root 26853 26796  45   Feb 21 ?        6539:10 /usr/apps/client/bin/top -n 1 -q
    root 26859 26796   0   Feb 21 ?           0:00 /usr/bin/sed s/^[       ]*//;s/[        ]*$//
    root 26857 26796   0   Feb 21 ?           0:00 /usr/bin/awk -F; {print $2}
    root 26796 26793   0   Feb 21 ?           0:00 /bin/ksh /usr/apps/client/bin/gatherOSKPI.sh
    root 26854 26796   0   Feb 21 ?           0:00 /usr/bin/head -n 5
    root 26858 26796   0   Feb 21 ?           0:00 /usr/bin/awk -F: {print $2}
    root 26858 26796   0   Feb 21 ?           0:00 /usr/bin/awk -F: {print $2}
   root 26858 26796   0   Feb 21 ?           0:00 /usr/bin/awk -F: {print $2}
    root 23755 23721   0   Feb 23 ?           0:00 /usr/bin/awk -F, {print $3,$4}
    root 23721 23720   0   Feb 23 ?           0:00 /bin/ksh /usr/apps/client/bin/gatherOSKPI.sh
    root 23748 23721  46   Feb 23 ?        3300:18 /usr/apps/client/bin/top -n 1 -q
    root 23720   287   0   Feb 23 ?           0:00 sh -c /usr/apps/client/bin/gatherOSKPI.sh
    root 23756 23721   0   Feb 23 ?           0:00 /usr/bin/awk -F  {print $1","$4}
    root 23751 23721   0   Feb 23 ?           0:00 /usr/bin/head -n 5

Note that sometimes the script is launched with the flag "-c", other times not, and once preceded by "/bin/ksh"

The awk and head output are due to commands in the script.

Can anyone suggest why this behavior occurs?
Largely, I am being given a chance to advance, but need to find why these processes are not ending once their run is complete

Thanks in advance,

Marc
# 2  
Old 02-25-2013
Posting the script does help
# 3  
Old 02-25-2013
I suggest to source your .profile in your cron entry right before calling script:
Code:
* * * * * . ~/.profile; /path_to_your_script/your_script

# 4  
Old 02-25-2013
Sorry,

I did not post the script as I was not sure how much space I had.
Here it is:
Code:
# Set Default Paths
#
PATH=/usr/apps/client/bin:$PATH; export PATH
LD_LIBRARY_PATH=/usr/apps/client/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
NOSHOME=/usr/apps/client/bin; export NOSHOME

# Execute the NOS provided top program with CPU MEMORY and LOAD values to be extracted to temporary files
#
$NOSHOME/top -n 1 -q | /usr/bin/head -n 5 | /usr/bin/grep CPU | /usr/bin/awk -F"," '{print $1}' | /usr/bin/awk -F":" '{print $2}' | /usr/bin/awk -F" " '{print $1}' > /tmp/cpu.out
$NOSHOME/top -n 1 -q | /usr/bin/head -n 5 | /usr/bin/grep Memory | /usr/bin/awk -F"," '{print $1,$2}' | /usr/bin/awk -F":" '{print $2}' | /usr/bin/awk -F" " '{print $1","$4}' > /tmp/mem.out
$NOSHOME/top -n 1 -q | /usr/bin/head -n 5 | /usr/bin/grep Memory | /usr/bin/awk -F"," '{print $3,$4}' | /usr/bin/awk -F" " '{print $1","$4}' > /tmp/swap.out
$NOSHOME/top -n 1 -q | /usr/bin/head -n 5 | /usr/bin/grep load | /usr/bin/awk -F";" '{print $2}' | /usr/bin/awk -F":" '{print $2}' | /usr/bin/sed 's/^[       ] *//;s/[        ]*$//' > /tmp/loadavg.out

# Gather all data into single file and clean up
#
CPU=`cat /tmp/cpu.out`
MEM=`cat /tmp/mem.out`
SWAP=`cat /tmp/swap.out`
LOAD=`cat /tmp/loadavg.out`
HOST=`/usr/bin/hostname`
SDATE=`/usr/bin/date +%b-%d-%y`
TIME=`/usr/bin/date +%H:%M:%S`
# Configure date values to figure out proper storage of comma delimited values
#
typeset -i MONTH=`/usr/bin/date +%m`
MONTH=$(echo "$MONTH" | tr ' ')
typeset -i DAY=`/usr/bin/date +%d`
DAY=$(echo "$DAY" | tr ' ')
typeset -i YEAR=`/usr/bin/date +%Y`
YEAR=$(echo "$YEAR" | tr ' ')

# Create a variable FILE to concat variables into a single variable to test against
FILE=$MONTH"-"$YEAR-$HOST".dat"

# Check to see if file is empty. If not, populate the values, otherwise create the needed file and populate
#
if [ -e $NOSHOME/../data/OSKPI/$FILE ]
  then
    echo $HOST","$SDATE","$TIME","$CPU","$MEM","$SWAP","$LOAD >> $NOSHOME/../data/OSKPI/$MONTH-$YEAR-$HOST.dat
  else
    echo $HOST","$SDATE","$TIME","$CPU","$MEM","$SWAP","$LOAD > $NOSHOME/../data/OSKPI/$MONTH-$YEAR-$HOST.dat
  fi

# Remove all temporary files and exit
#
rm /tmp/cpu.out /tmp/mem.out /tmp/swap.out /tmp/loadavg.out
exit 0


Last edited by Marc G; 02-25-2013 at 04:52 PM..
# 5  
Old 02-25-2013
I don't see the shebang line in your script. My guess is that you forgot to write it, that's why you see
Code:
 sh -c <scriptname>

-- bourne shell is invoked as default interpreter.

Please include the shebang line:
Code:
 #!/bin/ksh

at the beginning of the first line and try again
If it still doesn't work as expected, you should tell us whether the script works as it should when you invoke it from command line (with no cron).
# 6  
Old 02-26-2013
What caught my eye is
Quote:
Code:
     UID   PID  PPID   C    STIME TTY         TIME CMD
    root 26853 26796  45   Feb 21 ?        6539:10 /usr/apps/client/bin/top -n 1 -q

top running for 6539 minutes i.e. 4,5 days? I guess the piped processes are not lingering but waiting for top to finish?
# 7  
Old 02-26-2013
ok,

First, mirni...
That was my cut and paste error.
The shebang is:
#!/bin/ksh

I just missed it in the cut and paste.

Thanks for pointing that out though!
Marc

---------- Post updated at 11:27 AM ---------- Previous update was at 11:22 AM ----------

Quote:
Originally Posted by RudiC
What caught my eye is top running for 6539 minutes i.e. 4,5 days? I guess the piped processes are not lingering but waiting for top to finish?
Yes, that would be an issue on a production box but we are beta testing the script on a test box as they're only considering allowing me to increase my duties.

What caught my eye is not only the continual run of the top, but the differences in how cron seems to be initiating the script.

Either it with a "sh -c" or it is straight out (as seen in the Feb 21 result)

My first thought is that the top is still running because one of the follow on commands manipulating top's output was encountering an issue. But I realized that would not make sense as top is already done before its output can be piped to the next command.

Then I began wondering if there is something that can gum up the pipe so top can't reach it?

Is that possible?

Marc
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. Shell Programming and Scripting

Cron to run 3rd Tuesday of every odd months

Hi, I need to schedule a script to run on the 3rd tuesday of every odd months at 9 am. min, hour would be - 0 9 month would be - 1,3,5,7,9,11 Can someone suggest how I can schedule it to 3rd tuesday? Thanks. (8 Replies)
Discussion started by: member2014
8 Replies

3. Shell Programming and Scripting

Script runs in command-line fine but times out in CRON?

Hi, I have a script that seems to run to completion when in the command-line, but when it is run using the cron, it seems to time out. They both start and run fine, but on the CRON it stops prematurely. The script hits an API every few seconds and grabs data. Does anyone have any idea on... (4 Replies)
Discussion started by: phpchick
4 Replies

4. AIX

Script runs in shell but not cron

We run some menu driven software that has the ability to batch menu paths and generate reports quickly. Normally you run a batch like: $ BATCH BATCHNAME The batch program then prompts you for the date you want the report run for. I got some help from some folks on IRC to do the following: BATCH... (2 Replies)
Discussion started by: herot
2 Replies

5. Shell Programming and Scripting

Shell Script runs good manually but not through Cron tab

Hello Every one, I have a shell script which is running fine manually, but its giving me hard time when running tru cron job. :wall:. Am using #!/usr/bin/ksh >echo $SHELL /usr/bin/ksh Cron Job is as below, it execues but dosent do what i want it to do. 47 15 * * *... (1 Reply)
Discussion started by: naren.chowdhary
1 Replies

6. Shell Programming and Scripting

CRON shell script only runs correctly on command line

Hi, I'm new to these forums, and I'm hoping that someone can solve this problem... To make things short: I have DD-wrt set up on a router. I'm trying to run a script in CRON that fetches the daily password from my database using SSH. CRON is set like so(in web interface): * * * *... (4 Replies)
Discussion started by: louieaw
4 Replies

7. Shell Programming and Scripting

Script runs fine, but not in a cron

Okay, I have the following script that runs fine from a command line as well as an executable .sh file. It just moves any file/folder with movie* in the name to a folder called _Movies. The issue I'm running into is when it's call from a cron. find /mnt/HD_a2/BT/complete -iname "movie.*" -exec... (4 Replies)
Discussion started by: sammyk
4 Replies

8. Solaris

SFTP errorcode 1 when run on cron but runs manually

I am trying to run a sript on cron to SFTP data to a company. Private and public keys are set up. When I run this manully it works fine, however it was failing when run on cron. I have narrowed down the problem - it fails at the code that says if the error code is 0 then continue . . . I... (2 Replies)
Discussion started by: Heidi.Ebbs
2 Replies

9. UNIX for Advanced & Expert Users

Old cron entry still runs, but shouldnt

Hello, I'm running OSF1 V4.0 alpha. We used to have a job running from the cron at 6am everyday. We have removed the job from the crontab file, but the job still runs at 6am everyday. The job was in the root's crontab file. I cant figure out why this job is still running after we removed it... (3 Replies)
Discussion started by: xadamz23
3 Replies

10. UNIX for Dummies Questions & Answers

What user runs cron?

I have a command that is found in /usr/ud51/bin called stopudt which safely stops idle database users (let's writes finish, etc). If I login as root and issue stopudt the process is stopped. I put a script in cron to run it and it says stopudt not found. /usr/ud51/bin is in root's PATH. ... (10 Replies)
Discussion started by: michieka
10 Replies
Login or Register to Ask a Question