LinuxQuestions.org
Help answer threads with 0 replies.
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 10-02-2006, 11:22 AM   #1
WRXSTi
Member
 
Registered: Oct 2003
Location: NH
Distribution: SUSE
Posts: 32

Rep: Reputation: 15
lsof redirect


I'm trying to redirect the output of lsof from fileA.sh called by a cronjob, but it fails to do so.

fileA.sh contains the following.
echo **** my active listings ****
lsof -l


crontab -u root -l
* * * * * /home/root/fileA.sh /home/root/fileB.log


fileB.log contains the echo output, but not the losf.... why ?

Last edited by WRXSTi; 10-02-2006 at 11:25 AM.
 
Old 10-02-2006, 11:36 AM   #2
jxi
Member
 
Registered: Feb 2003
Location: Richmond VA
Distribution: Slackware 11 -- CentOS 4.4
Posts: 115

Rep: Reputation: 15
maybe you need the redirection sign '>'

* * * * * /home/root/fileA.sh > /home/root/fileB.log

Or perhaps lsof is putting output in stderr, so,

* * * * * /home/root/fileA.sh 2>&1 > /home/root/fileB.log
 
Old 10-02-2006, 11:46 AM   #3
WRXSTi
Member
 
Registered: Oct 2003
Location: NH
Distribution: SUSE
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
* * * * * /home/root/fileA.sh 2>&1 > /home/root/fileB.log
no luck still not written to fileB.log


If I run from the cmd line

sh fileA.sh > fileB.log

it will write the output of lsof to fileB.log, however via cronjob it does not.

Last edited by WRXSTi; 10-02-2006 at 11:57 AM.
 
Old 10-02-2006, 12:11 PM   #4
jxi
Member
 
Registered: Feb 2003
Location: Richmond VA
Distribution: Slackware 11 -- CentOS 4.4
Posts: 115

Rep: Reputation: 15
ok first, not to be overly critical, but where did /home/root come from? You're free to have such a path, but it's just unusual. Assuming that's in fact present (as opposed to, say, /root), then what is the output of :

/home/root/fileA.sh
/home/root/fileA.sh 2>file.err ; cat file.err

are you running the bash shell? (echo $SHELL)
 
Old 10-02-2006, 12:16 PM   #5
Markie1006
LQ Newbie
 
Registered: Oct 2006
Distribution: Slackware
Posts: 12

Rep: Reputation: 0
Quote:
Originally Posted by WRXSTi
no luck still not written to fileB.log


If I run from the cmd line

sh fileA.sh > fileB.log

it will write the output of lsof to fileB.log, however via cronjob it does not.
Common mistake - the 2>&1 needs to go at the end.

Code:
 * * * * * /home/root/fileA.sh > /home/root/fileB.log 2>&1
From the O'Reilly Unix Power Tools book
Quote:
1. $ cat food 2>&1 >file

On the first command line, the shell sees 2>&1 first. That means "make the standard error (file descriptor 2) go to the same place as the standard output (fd1) is going." There's no effect because both fd2 and fd1 are already going to the terminal. Then >file redirects fd1 (stdout) to file. But fd2 (stderr) is still going to the terminal.

2. $ cat food >file 2>&1

On the second command line, the shell sees >file first and redirects stdout to file. Next 2>&1 sends fd2 (stderr) to the same place fd1 is going - that's to the file. And that's what you want.
 
Old 10-02-2006, 12:39 PM   #6
jxi
Member
 
Registered: Feb 2003
Location: Richmond VA
Distribution: Slackware 11 -- CentOS 4.4
Posts: 115

Rep: Reputation: 15
Quote:
the 2>&1 needs to go at the end.
thanks Markie1006! (I just confirmed it with a test. Makes sense actually)
 
Old 10-02-2006, 12:58 PM   #7
WRXSTi
Member
 
Registered: Oct 2003
Location: NH
Distribution: SUSE
Posts: 32

Original Poster
Rep: Reputation: 15
Yes I'm using bash shell.

/home/root is a actually /home/xroot, but used /home/root for simplicty... bad example.

so I changed the crojob to
* * * * * sh fileA.sh > fileB.log 2>&1

lsof still not written to fileB.log

*********** ACTIVE FILES
fileA.sh: line 3: lsof: command not found

again I can run it

./fileA.sh

fine from the cmd line.

Last edited by WRXSTi; 10-02-2006 at 01:13 PM.
 
Old 10-02-2006, 01:22 PM   #8
WRXSTi
Member
 
Registered: Oct 2003
Location: NH
Distribution: SUSE
Posts: 32

Original Poster
Rep: Reputation: 15
I changed to

/usr/sbin/lsof

in fileA.sh and it now works !! few...

thx guys !

Last edited by WRXSTi; 10-02-2006 at 01:24 PM.
 
Old 10-02-2006, 03:45 PM   #9
Markie1006
LQ Newbie
 
Registered: Oct 2006
Distribution: Slackware
Posts: 12

Rep: Reputation: 0
Just as an addendum.

cron typically doesn't import the environment the way a normal login shell does, so it won't have the PATH setup for instance.
For this reason, you should always fully qualify any external commands in scripts that are going to be run from cron.
 
  


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
C Library for lsof loki.guz_BR Linux - Security 2 01-08-2021 01:06 AM
lsof not listing 'all' open file crowster Linux - General 7 06-04-2006 06:41 AM
What to do when lsof fails.... SpacedCowboy Linux - Security 2 10-29-2005 02:39 PM
Lsof Is Useless?? Philip_38 Linux - Software 3 01-30-2005 09:24 AM
lsof: command not found ionian2k Linux - Newbie 1 12-26-2002 08:59 PM

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

All times are GMT -5. The time now is 01:04 AM.

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