LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2011, 02:48 AM   #1
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Rep: Reputation: 0
Talking 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 20
i 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!
 
Old 09-12-2011, 03:04 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

Looks like you are looking for a while loop? Something like the following pseudo-code:
Code:
while pidof screen is true
  do this
  sleep 20
Note that I am not on linux right now, so I can't really code this up and test it for you. Hope this helps!

Cheers,

Josh
 
Old 09-12-2011, 03:20 AM   #3
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
corp769, thanks for this tip.

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.
 
Old 09-12-2011, 03:30 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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.
 
Old 09-12-2011, 03:44 AM   #5
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
Uh, this is far more complicated than I just thought at first. Nevermind, I will check for these processes manually. Thanks!
 
Old 09-12-2011, 04:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure I understand what is so complicated? Did you not understand the while loop?
 
Old 09-12-2011, 04:22 AM   #7
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
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.
 
Old 09-12-2011, 04:26 AM   #8
cober
LQ Newbie
 
Registered: Feb 2009
Distribution: Debian 5.0
Posts: 14

Original Poster
Rep: Reputation: 0
Full script is:
Code:
sab restart
antidelete
# Naredi screenshote brez da zajame rar fajle
find /home/cober/downloads/*/* -not -name "*.rar" | while read file; do
  mtn -f /usr/local/tahomabd.ttf -r 3 -w 900 "$file"
done
if [ "$(pidof SCREEN)" ]
then
echo was found.
sleep 20
==> my issue <== (if proc exists, check once more, if exists, once more etc., otherwise proceed.)
else
find /home/cober/downloads/*/* -maxdepth 1 -not -size +2G -not -name "*.jpg" | while read file; do
  ncftpput -b -u username -p pass ftp.domain.com . "$file"
fi
done
echo "Slike....:"
getm
du -h *
antidelete:
Code:
for FILENAME in /home/cober/downloads/*/*

        do
        cd ${FILENAME%/*}
        RARNAME=`dd if=/dev/urandom count=128 bs=1 2>&1 | md5sum | cut -b-16`; screen -d -m -S 1 rar a -hppassword -m0 -v2147483648b $RARNAME.rar "$1";

   done

exit 0
 
Old 09-12-2011, 05:49 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So if I understand correctly you wish to have the if be continuous until SCREEN does not exist ... yes?

In that case employ the loop that was suggested in place of the if and remove the else:
Code:
while pidof SCREEN; do sleep 20; done

find /home/cober/downloads/*/* -maxdepth 1 -not -size +2G -not -name "*.jpg" | while read file; do
  ncftpput -b -u username -p pass ftp.domain.com . "$file"; done
This will loop and pause for 20 seconds each time before checking again to see if SCREEN is running.
 
Old 09-12-2011, 06:42 AM   #10
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: Need help with an if statement to check if a file exists or not. MTAS Programming 16 10-26-2010 12:08 PM
[SOLVED] bash - check if file exists in tree hashbang#! Programming 10 01-21-2010 01:37 PM
using if in bash script to check if prog exists karuna-bdc Linux - Newbie 6 06-21-2009 07:23 PM
Bash Help: Check if file exists richinsc Programming 6 01-09-2009 12:27 PM
bash check folder exists zerocool22 Programming 22 06-01-2008 07:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration