title: bash script run to send process to background
body: <p>Hi Im making a script to do some rsync process, for the rsync process, Sys admin has created the script, when it run it is asking select options, so i want to create a script to pass that argument from script and run it from cron. </p>
  
  <p>list of directories to rsync take from file.</p>
  
  <pre><code>filelist=$(cat filelist.txt)
  
  for i in filelist;do
      echo -e "3\nY" | ./rsync.sh $i
  </code></pre>
  
  <p>#This will create a rsync log file
   so i check the some value of  log file and if it is empty i moving to the second file. if the file is not empty, i have to start rsync process as below that will take more that 2 hours.</p>
  
  <pre><code>if [ a != 0 ];then 
        echo -e "3\nN" | ./rsync.sh $i
  </code></pre>
  
  <p>above rsync process need to send to the background and take next file to loop. i check with the screen command, but screen is not working with server. also i need to get the duration that take to run process and passing to the log, when i use the time command i am unable to pass the echo variable. Also need to send this to background and take next file. appreciate any suggestions to success this task.  </p>
  
  <p>Question
  1. How to send  argument with Time command 
  echo -e "3\nY" | time ./rsync.sh $i
  above one not working </p>
  
  <ol start="2">
  <li>how to send this to background and take next file to rsync while running previous rsync process. </li>
  </ol>
  
  <p>Full Code</p>
  
  <pre><code>#!/bin/bash
  filelist=$(cat filelist.txt)
  Lpath=/opt/sas/sas_control/scripts/Logs/rsync_logs
  date=$(date +"%m-%d-%Y")
  timelog="time_result/rsync_time.log-$date"
  
  for i in $filelist;do
          #echo $i
          b_i=$(basename $i)
          echo $b_i
          echo -e "3\nY" | ./rsync.sh $i
          f=$(cat $Lpath/$(ls -tr $Lpath| grep rsync-dry-run-$b_i | tail -1) | grep 'transferred:' | cut -d':' -f2)
          echo $f
          if [ $f != 0 ]; then
                  #date=$(date +"%D : %r")
                  start_time=`date +%s`
                  echo "$b_i-start:$start_time" &gt;&gt; $timelog
                  #time ./rsync.sh $i &lt; echo -e "3\nY" 2&gt; "./time_result/$b_i-$date" &amp;
                  time { echo -e "3\nY" | ./rsync.sh $i; } 2&gt; "./time_result/$b_i-$date"
                  end_time=`date +%s`
                  s_time=$(cat $timelog|grep "$b_i-start" |cut -d ':' -f2)
                  duration=$(($end_time-$s_time))
                  echo "$b_i duration:$duration" &gt;&gt; $timelog
  
          fi
  
  done
  </code></pre>