Solved

CSH Run 2 things at once

Posted on 2011-03-25
11
399 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
ID: 35219321
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
ID: 35219341
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
ID: 35219521
It complains about "too many ('s"  when using the ()&. I think that might be because I am using CSH.

Thanks
Fixitben
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 35219774
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
ID: 35244497
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35244674
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
ID: 35244889
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
ID: 35244923
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
ID: 35244933
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
ID: 35246983
Assigned to me by mistake
0
 
LVL 7

Author Closing Comment

by:fixitben
ID: 35247002
Thanks sorry about the points.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

856 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