LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2008, 04:18 PM   #1
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Rep: Reputation: 15
single command in shell script not working


I have a shell script that is giving me a headache. It logs into a device runs a command and copies the output to a file. then an if grep "$sometrigger" test puts the network name of the device into a file. I then run "sort -u" on the file containing the list of device names that tested positive and start the process over with the list that tested positive the first pass to help eliminate false positives. I run "sort -u" on the list created by the second pass and put that into my final output file. I put this all together and tested in and it worked, then I added it to roots crontab to run every afternoon (I know I know it shouldn't be done as root)

the issue I am having is that neither "sort -u" command ever seems to work when run by cron. I checked the shebang and it is bash, the same shell I wrote and tested the script in. I ran "which sort" as both root and the user I wrote and tested as and the path is the same. I tried using the absolute path to sort even though which indicated it was using the same utility. I tried sorting the file and then piping to uniq separately, uniq didn't work because the file never sorted.

can someone please point me in the right direction so I can figure out why this script refuses to sort any of the files it has created?
 
Old 03-31-2008, 04:23 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by farkus888
the issue I am having is that neither "sort -u" command ever seems to work when run by cron.
The most common issues with cronjobs are related to its limited PATH.

So, either build PATH at the start of your script, or use the FQ path to the sort program.

---------------------------

edit: I missed this at first read:
Quote:
Originally Posted by farkus888
I tried using the absolute path to sort...
Are you sure you tried that? If so, next I'd turn on trace (set -x) near the start of the script to see what's happening. You'll get stdout/stderr from the cronjob sent to the user's mail.

Last edited by anomie; 03-31-2008 at 04:25 PM.
 
Old 03-31-2008, 04:27 PM   #3
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
Scripts run from cron do not have any system wide environment variables; such as PATH. The variables needed must be explicitly defined within your script. However, you can use the absolute path name to each of the executables executed within your cronjob, but I find it easier to echo $PATH from the shell I am testing the script in and include this within the script. ie,

# echo $PATH
/usr/sbin:/usr/ccs/bin

#! /bin/sh
export PATH=/usr/sbin:/usr/ccs/bin

Gentoo

Last edited by prad77; 04-17-2008 at 03:32 AM.
 
Old 03-31-2008, 04:28 PM   #4
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Original Poster
Rep: Reputation: 15
I have tried using the absolute pathname to the sort utility that I want used. I have all the cron jobs on this server set up to send all of their stdout and stderr to /dev/null to keep things cleaned up. I am in the process of running the script by hand as both accounts to see if any of the output differs right now. unfortunately its a time consuming process because of waiting for the output from 800+ devices.

Last edited by farkus888; 03-31-2008 at 04:45 PM.
 
Old 04-01-2008, 12:11 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Perhaps you can use nice to higher the sleeping rate of sort. Do 'man nice' for more info.. Also use absolute pathnames please.

# nice -n 4 /path/sort -u
 
Old 04-01-2008, 01:31 PM   #6
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Original Poster
Rep: Reputation: 15
I have not taken the absolute pathnames for each call of sort out of the script yet because I know they can't hurt. I did get an error running the script by hand last night that I am looking into, I will update with the fix if I find one.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script/command for converting columns/table onto a single line skuz_ball Programming 9 11-30-2007 03:02 AM
command with shell script sunlinux Programming 2 12-28-2006 11:36 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
series-Write a single shell command to display the following output ricki Programming 2 07-31-2005 03:53 PM
Command in shell script Grassie Coetzee Linux - Software 1 03-13-2005 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration