[SOLVED] Checking Processes Are Started with Correct User
Linux - NewbieThis 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!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to create a script with will allow me to check if a specific process is being run by the correct user (in this case this would be root).
I have tried a few scripts but hasn't worked so far, my last attempt was:
You're not really getting a process count but to do it the way you have it you just need to redirect the output of the pgrep command to /dev/null:
Code:
process_count=`pgrep -u root -x Introscope_WebView.lax >/dev/null;echo $?`
if [ "$process_count" -eq 0]
then
process_user="Root"
else
process_user="Not Root"
fi
echo "<metric type="LongCounter" name="OS Processes|WebView User Process:Process Count" value="$process_count"/>"
echo "<metric type="StringEvent" name="OS Processes|WebView User Process:Process User" value="$process_user"/>"
exit 0
The reason is that without the redirect successful pgreps are giving 2 lines of output rather than the 1 you're expecting from the return code (echo $?).
I have tried your solution but it still comes back "not as root" I have double checked the ps- efl|grep java and it is defiantly ran by root as shown below:
The "-x" flag is specifying command name. In your latest post you're showing the command name is actually "java" and the Introscope stuff is just part of the command line but not actually the name. Use the "-f" flag instead of "-x" so it looks at the entire command line:
The output of cat /proc/pid/status provides a lot of information including UID(s).
True enough but then the OP would first have to find the pid then look at /proc/pid which would add possibly unnecessary steps. What he is trying to do to find the basic answer works with pgrep and the appropriate flags for his conditional. (I'm not sure what the intent of the echos after that are.)
PID=$(pgrep -u root -f Introscope_WebView.lax)
RC=$?
if [ $RC -eq 0 ]
then echo process_user="Root" and PID is $PID
else echo process_user="Not Root and PID is $PID"
fi
In the above I'm using $() to encapsulate the command rather than ``. The latter still works but is deprecated and less useful than the former especially where you need to nest commands.
The first line just gets the Process ID (PID) reported by the pgrep.
The second line gets the return code of the first line.
If return code is 0 is prints your original echo and adds the PID to it.
If return code is not 0 it prints your original echo but doesn't show the PID (because there won't be one since the first line only returns a PID if it is being run as root).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.