LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-27-2011, 03:07 PM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Rep: Reputation: 15
Question lsof output not redirecting in a cronjob


When I run this command as root, I get the correct output of all open files opened by by the process 5018

lsof -p 5018 > lsof_output.txt

However, when I put this in a cron job

56 13 * * * lsof -p 5018 2>&1 > /root/lsof_output.txt

no output is written to lsof_output.txt

I confirmed that the job runs, by actually deleting the file before the scheduled time. A new empty file is created with no content. The process 5018 is active at the time the cron job runs.

What am I missing?

Thanks in advance.
 
Old 09-27-2011, 03:41 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
Try using the fully-qualified path to lsof(8). If you're not sure where it is, find out with:
Code:
# whereis lsof
 
1 members found this post helpful.
Old 09-27-2011, 04:01 PM   #3
threezerous
Member
 
Registered: Jul 2009
Posts: 89

Original Poster
Rep: Reputation: 15
Thank you....that helped
 
Old 09-27-2011, 06:39 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
Quote:
Originally Posted by threezerous View Post
What am I missing?
...I'd say you're missing more than just the cron PATH inclusion:
- when listing things always look for switches like "-n" as they may speed up processing by not translating. For lsof it's "-Pwln".
- using "-p $PID" is not extensible. Could pick up the actual PID with for example something like 'pgrep' ('man pgrep').
- root is no ordinary user and as such you shouldn't use it for write ops: use /tmp (or /dev/shm) for scratch files, /var/log for log files or 'logger' to pipe output into say syslog. If you're writing temporary files try using 'mktemp'.

Put together:
Code:
#!/bin/bash --
umask 0027; TEMPFILE=`mktemp /dev/shm/lsof.XXXXXXXXXXXX` && {
 pgrep sendmail 2>/dev/null|xargs -iP /usr/sbin/lsof -Pwlnp 'P' > "${TEMPFILE:/tmp/$$}" 2>&1
 [ -s "${TEMPFILE:/tmp/$$}" ] && install -o root -g root -m 0640 "${TEMPFILE:/tmp/$$}" "/var/log/lsof_$(/bin/date +'%Y%m%d').log"
 rm -f "${TEMPFILE:/tmp/$$}"
}; exit 0

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }

Last edited by unSpawn; 09-27-2011 at 06:40 PM.
 
  


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
lsof output description mayrus9 Red Hat 4 07-22-2014 02:45 PM
Email Myself Output of Cronjob? carlosinfl Linux - Server 15 05-21-2010 12:47 PM
how to read lsof output command many file open by root ??? b-RAM Linux - Newbie 2 10-05-2009 03:40 AM
'lsof' doesn't work in cronjob BlackHatRob Linux - Software 2 03-07-2008 05:03 PM
Active connections conundrum involving lsof / netstat output uncle-c Linux - Newbie 8 12-09-2007 11:56 AM


All times are GMT -5. The time now is 06:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration