Achive "NOT RUN PARALLEL"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Achive "NOT RUN PARALLEL"
# 1  
Old 12-23-2013
Achive "NOT RUN PARALLEL"

I have 4 scripts say SA , SB ,SC AND SD..I need to achieve the conditions below via CRONTAB:
1) SA will trigger by SB
2) SA should not run in parallel with SC and SD.
3) IF any of SC OR SD is RUNNING SA will wait for them to complete and run after their completion. Same for SC and SD ( IF SA is RUNNING)

I think it can be achieve by grepping the process in each script and wait till no process found ( I am not sure if wait will work here),but I have restriction to change the script so if this can be done with crontab it will be GREAT.

Thanks Much in advance.
# 2  
Old 12-24-2013
You are trying to set up something that allows process synchronization. This is usually achieved using some kind of IPC mechanism (Inter-process communication). You can use a lockfile.

Let's have three scripts: SA, SB, SC. you need to have a shell function or a bit of code to implement this. It is easier to control if each process uses PRECISELY the same mechanism to control its successors. Consider a lockfile.

crontab: run all three scripts at 2:20 am (example)

Code:
19 2 * * *  > /path/to/lockfile && echo 'startSA' > /path/to/lockfile
20 2 * * *  /path/to/SA >   /path/to/logfile
21 2 * * *  /path/to/SB >> /path/to/logfile
22 2 * * *  /path/to/SC >> /path/to/logfile

SA:
Code:
export start_parm='startSA'
while true
do
   grep -q "$start_parm" /path/to/lockfile
   [ $?  -eq 0 ]  && break
   sleep 1
done
# script code goes here
# ....
# ....
#
# last line of code:
echo 'startSB' > /path/to/lockfile
exit

SB:
Code:
export start_parm='startSB'
while true
do
   grep -q "$start_parm" /path/to/lockfile
   [ $?  -eq 0 ]  && break
   sleep 1
done
# script code goes here
# ....
# ....
#
# last line of code:
echo 'startSC' > /path/to/lockfile
exit

SC:
Code:
export start_parm='startSC'
while true
do
   grep -q "$start_parm" /path/to/lockfile
   [ $?  -eq 0 ]  && break
   sleep 1
done
# script code goes here
# ....
# ....
#
# last line of code:
echo 'endALL' > /path/to/lockfile
exit

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-24-2013
Thanks JIm

So that means I need to change my script? NO other option available?
# 4  
Old 12-25-2013
You could write a script that will run the other SB, SA, SC, and SD guaranteeing that SA completes before SC and SD start, allowing SB, SC, and SD to run in parallel, and guaranteeing that only one copy of the script runs at a time without modifying SA, SB, SC, or SD. One way to do this is:
Code:
#!/bin/ksh
# The following cd command must specify an absolute pathname to run it from cron.
cd '/directory/where/this/script/should/run'
IAm=${0##*/}
Lock="$IAm.lock"
set -C  # Generate error if redirection target exists.
# Create lock file, if it does not exist...
if ! date "+$IAm started %x at %X with PID $$" > "$Lock"
then    # Exit if the lock file already existed...
        printf "%s: Error: Another copy of this script is already running.\n" \
                "$IAm" >&2
        cat "$Lock" >&2
        exit 1
fi
trap 'rm -f "$Lock"' 0    # Set trap to remove the lock file on exit.
set +C
./SB&           # Start SB
./SA&           # Start SA
SApid=$!        # Get pid of SA
wait $SApid     # wait for SA to finish
./SC& ./SD&     # Start SC and SD
wait            # wait for SB, SC, and SD to finish

Note that when started by cron, your environment will not be initialized as it would be if you had logged in and started it from your shell. If SA, SB, SC, and SD need to have any environment variables set, you will need to set those variables in the start of this script before they are invoked.

This script exits if another copy of the script is still running. If you want it to enter an idle loop until the prior script completes, you can replace the if, then, else with a while, do, done and the exit with a sleep.

This script was tested with both ksh and bash. The way it sets $IAm will not work with an original Bourne shell. (There may also be other standard features used by this script that the Bourne shell does not support.)
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-27-2013
Thanks Don.. So I have two options now , one to change script with grepping process ID before start and one is to create a new script ... Let me see which will be feasible in my case.. Thanks much and YES this is the only site where I get my answer always...
# 6  
Old 04-03-2014
After you have modeled your dependencies using the lock modes developed by Distributed Lock Manager (see Wikipedia) you could use FLOM (Free LOck Manager) to achieve your result.

With FLOM you are able to execute a command specifying an abstract resource and a lock mode for every command:

flom --resource=XX --lock-mode=PR -- mycommand1
flom --resource=XX --lock-mode=EX -- mycommand2

and so on.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Slow printing / CUPS - process "parallel" high cpu

Hello. I have a slackware system running cups with an HP laserJet 2100 connected via parallel port in ECP mode. Print jobs are working. Very slowly. 15K test print out of cups takes about 2 minutes to complete. When the printout is on the way to the printer, the process "parallel" uses... (0 Replies)
Discussion started by: agentrnge
0 Replies

7. Shell Programming and Scripting

catalina.sh : need combination from "start" and "run"

heya, can someone help me with following problem. i am not sure how far you know the catalina.sh script from tomcat. when i start my tomcat with "catalina.sh run" then the startup-process-output will be printed out on the console, but the tomcat process is started in current shell/session, so... (1 Reply)
Discussion started by: Filly
1 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Run away "bootpgw" & "inetd"

Hello All. I'm get the following messages posted to the /var/adm/syslog file ever second and not sure on how to stop the process. May 14 15:50:52 a3360 bootpgw: version 2.3.5 May 14 15:50:52 a3360 inetd: /etc/bootpgw exit 0x1 As said about this gets logged every second only thing that... (4 Replies)
Discussion started by: cfaiman
4 Replies
Login or Register to Ask a Question