We help IT Professionals succeed at work.

can the "lsof" command be run by non-root users?

lkrubner asked
Last Modified: 2008-07-23
I've got a cron script, written in PHP, that does this call:

lsof | greg vsftpd

and saves the output in a text file.

When I ssh to the server and login as root and call this script myself, everything works fine. That is, if I do this:


I get the full list of files opened by vsftpd, saved to a text file, just like I would expect.

When the cron calls this script, it creates the file, like it should, but the file is empty.

Why is that? Is lsof limited to root users or something?

This is the PHP script that I'm calling as a cron, every 5 minutes:

$stringListingAllProcesses = shell_exec("lsof | grep vsftpd");
$filename = "/usr/local/bin/bluesugar_content_ingester/ftp_processes_" . time();
file_put_contents($filename, $stringListingAllProcesses);
chmod($filename, 0777);

This should cause files to be created in the folder "bluesugar_content_ingester". And the files are created. When I'm logged in as root, they are full of the output of lsof command. But when the script is called as a cron, the file is created, yet it is blank. It's as if there is no output.

Why would that be?

Watch Question

David BeveridgeLinux Systems Admin

lsof on my system is in the /usr/sbin folder whichi is only accessible by root.

David BeveridgeLinux Systems Admin

why not put it on roots crontab
Top Expert 2007

You can run it as wwwrun, but it will only list the files which are used by processes owned by the wwwrun user, not all files like it's done by root.
Top Expert 2005

> lsof on my system is in the /usr/sbin folder whichi is only accessible by root.
LOL; it's accessible by regular user as well. It's just (like hernst42) mentioned, it will list only those descriptors You have right to see.


ravenpl, that would explain why it gets nothing in return. I was thinking cron couldn't run lsof, so I ran visudo and added these lines to /etc/sudoers:

ALL  ALL = NOPASSWD: /usr/sbin/lsof
All  All = NOPASSWD: /bin/grep

I did "whereis lsof" and "whereis grep" to get the paths for these commands.
 But still nothing works. Your explanation makes sense.

Is there a way to make this script run as root?


bevhost, I was logged in as root when I ran "crontab -e" so the cron job should be listed in the root user's crontab.
Top Expert 2005
This one is on us!
(Get your first solution completely free - no credit card required)
David BeveridgeLinux Systems Admin

PATH environment is not set inside Cron so perhaps you just need to prefix the lsof command with it's full path


ravenpl, thanks. That was the problem. I needed to use the full path. cron is different from calling things from the command line.

I used "which lsof" to find the path.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.