Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-27-2011, 03:07 PM   #1
Registered: Jul 2009
Posts: 96

Rep: Reputation: 16
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
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:
# whereis lsof
1 members found this post helpful.
Old 09-27-2011, 04:01 PM   #3
Registered: Jul 2009
Posts: 96

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

Rep: Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565
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:
#!/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

function help() { echo "Bash scripting guides:"; }

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


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:18 PM.

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