0

I want to search files by using grep command in the directory /var/run

/var/run stores the processes running in the system and it has files with pid extension. I want to get a list of all the files with the extension pid .

Command I am using sudo grep -nr '*.pid' . (I am executing this command from the directory /var/run )

It shows no output. I am using Ubuntu 14.04 LTS.

1 Answers1

1

TL;DR: grep is wrong tool, use find with correct options

If you do stat /var/run you'll quickly find out that /var/run is symlink to /run directory.

$ stat /var/run
  File: /var/run -> /run
  Size: 4           Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d  Inode: 696874      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-02-07 13:17:01.225178554 +0800
Modify: 2017-12-18 20:44:12.898113636 +0800
Change: 2017-12-18 20:44:12.898113636 +0800

So you really need /run directory instead. As for searching files with specific filename, you need find command:

$ find /run -name "*.pid" 
/run/charon.pid
/run/starter.charon.pid
/run/dhclient-wlp2s0.pid

Because some files in that directory belong to root or other system users, you may need to use that command with sudo.

Alternatively, you can use -L flag to allow following symlinks and call find on /var/run:

$ find -L  /var/run -name "*.pid" 
/var/run/charon.pid
/var/run/starter.charon.pid
/var/run/dhclient-wlp2s0.pid

Please note also, that grep is wrong tool for the job. grep is for searching text patterns inside text files, not in their filenames.

You also mentioned:

/var/run stores the processes running in the system and it has files with pid extension

That's actually incorrect. Process information belongs in /proc. The .pid files are simply used by some programs to prevent multiple copies of same process running (well, one of possible ways these files can be used). See this stackoverflow post for reference, as well as this highly voted answer on unix.se. While the directory belongs to root user, please don't assume that it's for startup and daemon apps only; scripts initiated with root permissions by user could write to the directory just as easily.

  • Using /var/run/ will avoid the need for -L. Also bear in mind that Ubuntu 14 LTS uses Upstart, and the thing that actually prevents multiple copies of the same program running as multiple processes is Upstart knowing not to start them. – JdeBP Feb 07 '18 at 06:50
  • @JdeBP Sorry, find /var/run alone doesn't work in my case. find --version reports find (GNU findutils) 4.7.0-git. So at least with my version of GNU find it does need -L flag. I am not sure about how BSD find handles things. Nice to know that about 14.04 Ubuntu's Upstart feature, though that doesn't help with scripts which want to avoid multiple copies of self running, so at least on askubuntu we have been suggesting to use .pid files for that. – Sergiy Kolodyazhnyy Feb 07 '18 at 06:54
  • You speed-read what I wrote. Read it again slowly, and note the exact sequence of characters. (-: – JdeBP Feb 07 '18 at 07:38
  • @JdeBP Oh...Adding final slash works... – Sergiy Kolodyazhnyy Feb 07 '18 at 08:35