[SOLVED] BASH: if statement (check if process exists and then...)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
BASH: if statement (check if process exists and then...)
Hey all,
I have a little problem with my script, so I'm asking you to help me.
First of all, I want to pack all files (*/*) in RAR and since RAR isn't supporting multithreading in *NIX I wrote this script.
rar:
Code:
for FILENAME in */*
do
cd ${FILENAME%/*}
RARNAME=`dd if=/dev/urandom count=128 bs=1 2>&1 | md5sum | cut -b-16`; screen -d -m rar a -hppassword -m0 -v2147483648b $RARNAME.rar "$1";
done
exit 0
Ok, this one is going to run in background using screen so I'll be able to use more than 1 CPU for adding files to rar.
Now, where's my problem (and my lack of knowledge) appears..
I also made a script which checks if screen process is running
Code:
if [ "$(pidof SCREEN)" ]
then
echo was found.
else
echo not found.
fi
Now please help
My idea:
Code:
if [ "$(pidof SCREEN)" ]
then
echo was found.
sleep 20i want to check it again in 20sec and if there is no more screen process to proceed, otherwise to check again in 20sec and on and on..
else
echo not found.
fi
Don't even know how is this called.. any help really appreciated!
I want to know how to do it repeatedly, like to check in my example 20sec interval, and to proceed if there's no screen process running, otherwise to just check every 20sec for a screen process.
Maybe what you could do is create an absolute true while loop, and have the if statement within it. From there, it will keep looping regardless because you are telling it to loop infinitely. Then within the while loop, have your for and sleep statements, and you should be set. Of course you would want to set it up where depending on whatever variable or what not, you will want to break out of the while statement.
grail: I'm not really into bash scripting, so it's not that easy for me. I thought there's just a simple way to make this work but since I lack some bash knowledge I'll just check manually 'ps -aux' and then run other script.
Going back to the beginning: why do you use screen for the processes, instead of running it in the background by appending an & at the end of the command? Then you can list the running instances with jobs and wait for the completion by a wait command in shell script.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.