Use pgrep command in an if statement - linux

I need to have a .sh file that will echo 0 if my python service is not running. I know that pgrep is the command I want to use, but I am getting errors using it.
if [ [ ! $(pgrep -f service.py) ] ]; then
echo 0
fi
Is what I found online, and I keep getting the error
./test_if_running.sh: line 3: syntax error near unexpected token `fi'
./test_if_running.sh: line 3: `fi;'
When I type
./test_if_running.sh

The issue in your code is the nested [ ... ]. Also, as #agc has noted, what we need to check here is the exit code of pgrep and not its output. So, the right way to write the if is:
if ! pgrep -f service.py &> /dev/null 2>&1; then
# service.py is not running
fi

This is a bit simple, but why not just print a NOT'd exit code, like so:
! pgrep -f service.py &> /dev/null ; echo $?
As a bonus it'll print 1 if the service is running.

Related

Binary operator expected error while running a while loop in bash

TL:DR
Check if a given PID is running, if yes kill the process.
count=0
while [[ "$count" -le 3 && ps -p $pid > /dev/null ]];
do
kill -9 $pid
count=$(( $count + 1 )):
done
To this I am getting an error as:
line 8: [: -p: binary operator expected
I am aware there are several similar questions, I already tried their solutions but it doesn't seem to work.
The while loop is logically incorrect, as #kvantour mentioned. Here is the script. Note that it will let you know if it could not kill the process, so you can investigate the root cause. The script gets PID as its first argument (e.g. $./kill-pid.sh 1234) Note that this works for bash ver. 4.1+:
#!/usr/bin/env bash
if ps -p $1 > /dev/null
then
output=$(kill -9 $1 2>&1)
if [ $? -ne 0 ]
then
echo "Process $1 cannot be killed. Reason:"
echo "$output"
# This line is added per OP request, to try to re-run the kill command if it failed for the first time.
# kill -9 $1
fi
fi

Stop grep message from posting

I am working on a script that take 1 string argument and a file. I want it so that if a file is put in that doesn't exist, then it will display the "filename cannot be read" message.
That part does work however it also displays a "grep: grep.txt: No such file or directory" message. Is there any way to stop the grep message from posting and ending the script if the first if statement is true?
#! /bin/sh
if [ ! -f "$2" ]
then
echo "$0" cannot be read 1>&2
fi
if [ $# -eq 2 ]
then
grep "$1" $2
else
echo there is more or less than 2 arguments 1>&2
fi
Exit the script with a non-zero exit code to indicate failure and stop it from continuing on to the grep.
if [ ! -f "$2" ]
then
echo "$0" cannot be read 1>&2
exit 1
fi
You can add /dev/null in grep command it will suppress the error part.
grep "$1" $2 2>/dev/null
The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
2> file redirects stderr to file
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
You could redirect all errors from grep, for example:
grep "$1" $2 2>/dev/null
(the 2> means redirect standard error, as opposed to standard output with > or 1>).
That introduces a race condition, however: if the file disappears while your script as running, it might still exist when you check that it exists, but be gone by the time grep runs.
You could handle that by checking the exit status...
grep "$1" $2 2>/dev/null
if [[ $? -gt 1 ]]; then
echo "grep failed unexpectedly" >&2
fi
IMHO, in this example it would be better to just let grep print the error.

Linux: Reading the output of readlink /proc/pid/exe within a Bash Script

So I am writing a bash script which will run through all of the process ids in /proc/[pid] and read the executable that was used to run it.
From what I have had a looked at, the /proc filesystem contains the /proc/[pid]/exe symbolic link. Within the bash script I am trying work out how to read the value of "readlink /proc/[pid]/exe" to check if (deleted) or nothing is returned to find out whether the original executable exists on the disk or not.
Is there a way of doing this, so far I have?
#!/bin/bash
pid = "0"
while [ $pid -lt 32769 ]
do
if [-d /proc/$pid]; then
if [-f /proc/$pid/exe]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid = $[$pid+1]
done
This fails to work and always returns nothing.I am trying to list all of the processes that no longer have their executables available on disk.
Will this work for you?
#!/bin/bash
for i in $(ls /proc | awk '/^[[:digit:]]+/{print $1}'); do
if [ -h /proc/$i/exe ]; then
echo -n "$i: "
if readlink /proc/$i/exe >/dev/null 2>&1 ; then
echo "executable exists"
else
echo "executable not found"
fi
fi
done
I've updated your script to make it work. Notice that -f checks whether a file name represents a regular file. I would return false for a symbolic link:
pid="0"
while [ $pid -lt 32769 ]
do
if [ -d /proc/$pid ]; then
if [ -h /proc/$pid/exe ]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid=$[$pid+1]
done
you can read returned value after any command in shell by printing $? variable:
readlink
echo $?
if link is invalid, $? will be bigger than 0.
however if link exist and actual file is deleted, you can use something like:
ls `readlink somelink`
readlink -f `ls --dereference /proc/$pid/exe`

Check if a variable has a value or null?

Below if condition is not working properly. Please advise.
I have java process id , but below working "No process alive". Why if [ -z $PIDS ] not working ?
PIDS= pgrep -f java
if [ -z "$PIDS" ]
then
echo "No process alive...."
exit 1
else
echo "An instace is running in background."
fi
If you want PIDS to contain the output of running that command, you need to tell the shell that:
PIDS="$(pgrep -f java)"
Your first line should read
PIDS="$(pgrep -f java)"

how to check whether one instance of shell script is already running - Linux

I have two different shell script say like
a.sh
b.sh
**code of a.sh**
#!/system/bin/sh
#some code
./xyz/b.sh &
Here we can see i am running b.sh through a.sh file which is postboot script. Each time when device gets reboot it is adding ./xyz/b.sh & which i am trying to avoid.
what i am trying to do :
i need to write a code in such a way that will find if ./system/xyz/b.sh & is already there then no need to add again.
Code :
if pgrep /xyz/b.sh > /dev/null 2>&1
then
echo aplog is running
exit 1
fi
these code is not running. Do not know where i am doing mistake.
Just try:
pgrep b.sh > /dev/null 2>&1
if [ 0 == $? ]
then
...
fi
pgrep will only work on process name, not full path to process name.
Try pgrep -f b.sh or pgrep -x b.sh instead of pgrep -x /xyz/b.sh
Hi test your file existence before creating it with:
filename="/fullpath/xyz/b.sh"
if [ -f "$filename" ]
then
echo "$filename found"
else
echo "$filename not found."
fi

Resources