Home > Back-end >  Using the ps command to list users who do not have processes running
Using the ps command to list users who do not have processes running

Time:11-16

I have prepared a file with a list of my system's user names using a standard file from the /etc/ directory. What script can I write to list names of users who do not have any processes running right now using the ps command and this file specifically?

CodePudding user response:

You can use a for loop to read through each line of the file and then use the ps command to check if any processes are running for that user. If there are no processes running, you can print out the user's name.

for user in $(cat /etc/passwd); do ps -u $user > /dev/null if [ $? -eq 1 ]; then echo $user; fi done

  • Related