Solved

CSH Run 2 things at once

Posted on 2011-03-25
11
385 Views
Last Modified: 2012-08-13
I have some code written is CSH.
The code backs up data to a drive then displays on the screen as each step finishes.
I need to back up the data to 2 drives for redundancy. So currently it backs up the data to the first drive then backs it up to the second drive. I would like to speed up the process by backing up to both at the same time.

Is there a way to spawn both at the same time and output any echos I have back to the terminal in CSH?

Below is a sample of the code I am using to give you an idea.

 
echo "Writing to Backup System 1 Please Wait"
    rsync -vlrptog  --stats  $dir/ $pnpath/$filedate > $pnpath/$filedate/$project-$filedate.log
    cp $pnpath/$filedate/$project-$filedate.log $dir/.
    cp $pnpath/$filedate/$project-$filedate.log /$drive/log/.
    echo "rsync -vlrptogn  --stats $dir/ $pnpath/$filedate" >> $pnpath/$filedate/$project-$filedate.log
    set bytes = `grep "Total file" $pnpath/$filedate/$project-$filedate.log | cut -d: -f2 | cut -d' ' -f2`
    set kilobytes = `echo "scale=3; $bytes/1024" | bc`
    set megabytes = `echo "scale=3; $kilobytes/1024" | bc`
    set gigabytes = `echo "scale=3; $megabytes/1024" | bc`

    mutt -c xxxxx@xxxx.com -s "Linux Backup Drive 1  $gigabytes gb   $project $filedate " xxxx@xxxx.com < $pnpath/$filedate/$project-$filedate.log
    echo ""
    echo " "
    echo " "
    echo "Drive 1 Complete $project"


echo "Writing to Backup System 2 Please Wait"
    rsync -vlrptog  --stats  $dir/ $pnpath/$filedate > $pnpath/$filedate/$project-$filedate.log
    cp $pnpath/$filedate/$project-$filedate.log $dir/.
    cp $pnpath/$filedate/$project-$filedate.log /$drive/log/.
    echo "rsync -vlrptogn  --stats $dir/ $pnpath/$filedate" >> $pnpath/$filedate/$project-$filedate.log
    set bytes = `grep "Total file" $pnpath/$filedate/$project-$filedate.log | cut -d: -f2 | cut -d' ' -f2`
    set kilobytes = `echo "scale=3; $bytes/1024" | bc`
    set megabytes = `echo "scale=3; $kilobytes/1024" | bc`
    set gigabytes = `echo "scale=3; $megabytes/1024" | bc`

    mutt -c xxxxxxxx@xxxx.com -s "Linux Backup Drive 1  $gigabytes gb   $project $filedate " xxxx@xxx.com < $pnpath/$filedate/$project-$filedate.log
    echo ""
    echo " "
    echo " "
   echo "Drive 2 Complete $project"

Open in new window



Thanks
Fixitben

0
Comment
Question by:fixitben
  • 6
  • 5
11 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Hi,

either turn your single script into two distinct scripts, one for each task, and start them (maybe in a wrapper script) like:

#!/bin/csh
echo "Start of Parallel Backup"
backup1.csh &
backup2.csh &
echo "End of Parallel Backup"
exit

or change your existing script like below.

Note the parentheses  "(  )"  around each of the two code blocks followed by "&"

Please note also that with either method the messages issued will overlap, so it might be better writing them to logfiles.

wmp
( echo "Writing to Backup System 1 Please Wait"
    rsync -vlrptog  --stats  $dir/ $pnpath/$filedate > $pnpath/$filedate/$project-$filedate.log
    cp $pnpath/$filedate/$project-$filedate.log $dir/.
    cp $pnpath/$filedate/$project-$filedate.log /$drive/log/.
    echo "rsync -vlrptogn  --stats $dir/ $pnpath/$filedate" >> $pnpath/$filedate/$project-$filedate.log
    set bytes = `grep "Total file" $pnpath/$filedate/$project-$filedate.log | cut -d: -f2 | cut -d' ' -f2`
    set kilobytes = `echo "scale=3; $bytes/1024" | bc`
    set megabytes = `echo "scale=3; $kilobytes/1024" | bc`
    set gigabytes = `echo "scale=3; $megabytes/1024" | bc`

    mutt -c xxxxx@xxxx.com -s "Linux Backup Drive 1  $gigabytes gb   $project $filedate " xxxx@xxxx.com < $pnpath/$filedate/$project-$filedate.log
    echo ""
    echo " "
    echo " "
    echo "Drive 1 Complete $project" ) &


( echo "Writing to Backup System 2 Please Wait"
    rsync -vlrptog  --stats  $dir/ $pnpath/$filedate > $pnpath/$filedate/$project-$filedate.log
    cp $pnpath/$filedate/$project-$filedate.log $dir/.
    cp $pnpath/$filedate/$project-$filedate.log /$drive/log/.
    echo "rsync -vlrptogn  --stats $dir/ $pnpath/$filedate" >> $pnpath/$filedate/$project-$filedate.log
    set bytes = `grep "Total file" $pnpath/$filedate/$project-$filedate.log | cut -d: -f2 | cut -d' ' -f2`
    set kilobytes = `echo "scale=3; $bytes/1024" | bc`
    set megabytes = `echo "scale=3; $kilobytes/1024" | bc`
    set gigabytes = `echo "scale=3; $megabytes/1024" | bc`

    mutt -c xxxxxxxx@xxxx.com -s "Linux Backup Drive 1  $gigabytes gb   $project $filedate " xxxx@xxx.com < $pnpath/$filedate/$project-$filedate.log
    echo ""
    echo " "
    echo " "
   echo "Drive 2 Complete $project" ) &

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Forgot to "wait" in the wrapper script, sorry!

#!/bin/csh
echo "Start of Parallel Backup"
backup1.csh &
backup2.csh &
wait
echo "End of Parallel Backup"
exit
0
 
LVL 7

Author Comment

by:fixitben
Comment Utility
It complains about "too many ('s"  when using the ()&. I think that might be because I am using CSH.

Thanks
Fixitben
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
Yes,

you're right, csh would not tolerate line breaks inside the parentheses
(I just tested it here, didn't know that up to now).

I can't see anything specific to csh in the code, so why don't you use bash?
Just put "#!/bin/bash" into the first line of your script, instead of "#/bin/csh".

If you insist in csh you could use the "two scripts" method.

You could also terminate each line inside the parentheses (including blank lines) with ";\", i.e. a command separator plus a line continuation character. I tested this as well, it works, but it seems a bit too tedious, don't you think?

 
( echo "Writing to Backup System 1 Please Wait" ;\
    rsync -vlrptog  --stats  $dir/ $pnpath/$filedate > $pnpath/$filedate/$project-$filedate.log ;\
    cp $pnpath/$filedate/$project-$filedate.log $dir/. ;\
    cp $pnpath/$filedate/$project-$filedate.log /$drive/log/. ;\
    echo "rsync -vlrptogn  --stats $dir/ $pnpath/$filedate" >> $pnpath/$filedate/$project-$filedate.log ;\
    set bytes = `grep "Total file" $pnpath/$filedate/$project-$filedate.log | cut -d: -f2 | cut -d' ' -f2` ;\
    set kilobytes = `echo "scale=3; $bytes/1024" | bc` ;\
    set megabytes = `echo "scale=3; $kilobytes/1024" | bc` ;\
    set gigabytes = `echo "scale=3; $megabytes/1024" | bc` ;\
;\
    mutt -c xxxxx@xxxx.com -s "Linux Backup Drive 1  $gigabytes gb   $project $filedate " xxxx@xxxx.com < $pnpath/$filedate/$project-$filedate.log ;\
    echo "" ;\
    echo " " ;\
    echo " " ;\
    echo "Drive 1 Complete $project" ) &


( echo "Writing to Backup System 2 Please Wait" ;\
    rsync -vlrptog  --stats  $dir/ $pnpath/$filedate > $pnpath/$filedate/$project-$filedate.log ;\
    cp $pnpath/$filedate/$project-$filedate.log $dir/. ;\
    cp $pnpath/$filedate/$project-$filedate.log /$drive/log/. ;\
    echo "rsync -vlrptogn  --stats $dir/ $pnpath/$filedate" >> $pnpath/$filedate/$project-$filedate.log ;\
    set bytes = `grep "Total file" $pnpath/$filedate/$project-$filedate.log | cut -d: -f2 | cut -d' ' -f2` ;\
    set kilobytes = `echo "scale=3; $bytes/1024" | bc` ;\
    set megabytes = `echo "scale=3; $kilobytes/1024" | bc` ;\
    set gigabytes = `echo "scale=3; $megabytes/1024" | bc` ;\
;\
    mutt -c xxxxxxxx@xxxx.com -s "Linux Backup Drive 1  $gigabytes gb   $project $filedate " xxxx@xxx.com < $pnpath/$filedate/$project-$filedate.log ;\
    echo "" ;\
    echo " " ;\
    echo " " ;\
   echo "Drive 2 Complete $project" ) &

Open in new window

0
 
LVL 7

Author Comment

by:fixitben
Comment Utility
Is there a way to direct the std out back to the parent process?

Attached is the output.


I would like to see the entire output of  (echo "Writing to Primary Backup System Please Wait"; rsync -vlrptog --stats  ... )

Thanks
Fixitben
Creating Folder Structure for Primary Drive
Folder Created for Primary Drive
Creating Folder Structure on Secondary Drive
Folder Created for Secondary Drive Starting Backup

Writing to Primary Backup System Please Wait
[1] 15994

Writing to Secondary Backup System Please Wait
[2] 15996
[2]    Done                          ( echo "Writing to Secondary Backup System Please Wait"; rsync -vlrptog --stats ...
[1]  + Done                          ( echo "Writing to Primary Backup System Please Wait"; rsync -vlrptog --stats  ...



Complete 114418

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
What do you mean with "entire output"?

There is generally no need to redirect anything back, stdout is used and displayed as always.

Nearly all of your output is redirected to logfiles (if you use the snippet I posted),
except for "Writing to ..." which is indeed displayed, and the "Drive complete .." stuff - which is somewhat incomplete, but this should not happen! Did you actually use the code I posted?

wmp

0
 
LVL 7

Author Comment

by:fixitben
Comment Utility
Yeah that will work I added the echo "Drive 1 Complete $project"  to each and that helps.
I was worried that the errors where going to get lumped into the one line and I wouldn't be able to see them.

Thanks for your help.

Thanks
Fixitben
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Didn't you find (one or several of) my comments helpful?

Why would you accept your own answer as a solution?
0
 
LVL 7

Author Comment

by:fixitben
Comment Utility
I messed up I am submitting a request to have it changed. I clicked mine thinking it was yours.

Sorry I will get it fixed.

Thanks
Fixitben
0
 
LVL 7

Author Comment

by:fixitben
Comment Utility
Assigned to me by mistake
0
 
LVL 7

Author Closing Comment

by:fixitben
Comment Utility
Thanks sorry about the points.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now