Check if a variable has a value or null? - linux

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)"

Related

Cant print PID of a process

I'm trying to print the PID of running processes with the "lsof" command.
It seems that i do get the correct PID, but i can't print it with the "echo" command.
Regarding the following bash script:
#!/bin/bash
portsArr=(4003 4200)
for port in "${portsArr[#]}"
do
PID_OF_EXISTING_PROCESS=$(lsof -ti:"$port")
if [ -z "$PID_OF_EXISTING_PROCESS" ];
then
echo "Process was already running on port: $PID_OF_EXISTING_PROCESS"
fi
done
The output is
Process was already running on port:
Which means that a PID was identified (the "if" command reached that point plus i did some more echo and i saw that a the PID_OF_EXISTING_PROCESS was equal to 4003 at that point) , but for some reason wasn't printed in the "echo" command.
if [ -z "$PID_OF_EXISTING_PROCESS" ];
This test only passes if the string is empty. Use -n to verify that it's not empty.
if [ -n "$PID_OF_EXISTING_PROCESS" ];

Use pgrep command in an if statement

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.

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`

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

Bash script to check multiple running processes

I'm made the following code to determine if a process is running:
#!/bin/bash
ps cax | grep 'Nginx' > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
I would like to use my code to check multiple processes and use a list as input (see below), but getting stuck in the foreach loop.
CHECK_PROCESS=nginx, mysql, etc
What is the correct way to use a foreach loop to check multiple processes?
If your system has pgrep installed, you'd better use it instead of the greping of the output of ps.
Regarding you're question, how to loop through a list of processes, you'd better use an array. A working example might be something along these lines:
(Remark: avoid capitalized variables, this is an awfully bad bash practice):
#!/bin/bash
# Define an array of processes to be checked.
# If properly quoted, these may contain spaces
check_process=( "nginx" "mysql" "etc" )
for p in "${check_process[#]}"; do
if pgrep "$p" > /dev/null; then
echo "Process \`$p' is running"
else
echo "Process \`$p' is not running"
fi
done
Cheers!
Use a separated list of of processes:
#!/bin/bash
PROC="nginx mysql ..."
for p in $PROC
do
ps cax | grep $p > /dev/null
if [ $? -eq 0 ]; then
echo "Process $p is running."
else
echo "Process $p is not running."
fi
done
If you simply want to see if either one of them is running, then you don't need loo. Just give the list to grep:
ps cax | grep -E "Nginx|mysql|etc" > /dev/null
Create file chkproc.sh
#!/bin/bash
for name in $#; do
echo -n "$name: "
pgrep $name > /dev/null && echo "running" || echo "not running"
done
And then run:
$ ./chkproc.sh nginx mysql etc
nginx: not running
mysql: running
etc: not running
Unless you have some old or "weird" system you should have pgrep available.

Resources