Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I check if /proc/ is mounted?

  • Using /etc/mtab is discouraged as it might be inconsistent.

  • Using /proc/mounts is also not an option as might not exist if /proc/ is not mounted (although checking for its existence may be a way to do this check.

What is the best way to do this check?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

You can run the mount without any arguments to get a list of current mounts. The /etc/mtab file should have similar data, but like you said it is possible for this to be inconsistent with what is actually mounted in the event that the /etc file system is messed up, not writable, or another program has messed with it. You can get specific information about the proc mounts by asking mount to list all mounts of type proc like this:

mount -l -t proc

Edit: It looks like you can use stat to compare the device of the /proc folder to the device of / to tell at least if SOMETHING is mounted there other than the root file system:

[[ $(stat -c %d%D /proc) != $(stat -c %d%D /) ]] && echo "Something is mounted at /proc"
share|improve this answer
    
This is indeed one way to do it. The risk here is the running the mount command might get stuck on various problematic mounts like NFS/CIFS shares which are disconnected. –  freddie Jul 6 '11 at 11:48
    
@freddie: Does it actually get stuck if you ask it to list mounts of type proc like I showed? I don't have a case I can test right now, but if it's decently implemented I don't see why it would hang on scanning the status of nfs mounts if asked to list only proc mounts. Note that I did NOT recommend running mount | grep proc for just that reason. –  Caleb Jul 6 '11 at 11:51
    
I will need to test it, but I agree, if it's implemented properly, mount -l should not get stuck and in that case this is a good solution. Although, mount -l returns return code 0 if it finds a mount point (or more) or if it doesn't find it. So the check is further complicated by using mount -l -t proc |wc -l and checking how many filesystems are returned :( –  freddie Jul 6 '11 at 11:56
    
@freddie: see my edit for another solution. Also you could use a string test on the mount without running wc: [[ -n $(mount -l -t proc) ]] –  Caleb Jul 6 '11 at 12:06

Test for the existence of /proc/mounts. Running mount is no good because if /proc isn't mounted, it will return potentially obsolete data from /etc.

In theory there might be something else at /proc. But this is extremely unlikely in practice: if /proc/mounts exists and /proc isn't the proc filesystem, you can't trust anything about your environment anyway. If you're really worried, you can check that the filesystem type is proc: df -PT /proc | awk 'NR==2 && $2=="proc" {print 1}' (requires the Linux utilities df, there's no corresponding option in Busybox). Conversely, in theory, there could be a proc filesystem mounted in a different place; there's no easy way to find this with shell commands (df reads /proc/mounts to enumerate filesystems). In practice, just check for /proc/mounts.

share|improve this answer

If you want a (more or less) guarantee that the filesystem on /proc is in fact proc instead of some other filesystem or a directory structure made to look like it, you can call statfs() and check the type, 0x9fa0 is the value for proc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.