LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   running script in background ... (https://www.linuxquestions.org/questions/linux-newbie-8/running-script-in-background-4175437339/)

cfaxon1 11-15-2012 02:47 PM

running script in background ...
 
The ./script_name.sh& doesn't work for me.

I also tried it with a space between the script and the &: ./script_name.sh &

This didn't work either. My script calls and closes a GUI repeatedly, which doesn't bother me too much since
it just pops up behind the terminal window, but I would like to be free to move to other directories and continue my
work uninterrupted without opening multiple terminal windows.

Any ideas why this wouldn't be working?

cfaxon1 11-15-2012 03:30 PM

Hmmm....not working for me
 
[deleted]

Tinkster 11-15-2012 04:37 PM

I split your necro-posts out of the long-dead thread. Can you please
describe in what way the "doesn't work for me" manifests itself?



Cheers,
Tink

Habitual 11-16-2012 07:39 AM

post contents of script_name.sh please.

Please use [code][/code] tags.
Thank you,

cfaxon1 11-16-2012 12:01 PM

Here are the contents of my script

Code:

#!/bin/bash
#
# Run name
RUN=clv1_1000x_11.2.BLoss
# species to loop
SPECIES="CLV1 HCL"
# directory w/ data files
DATA_DIR="./"
# Days to loop
YYYYMMDD="20060531 20060601 20060602 20060603 20060604 20060605
    20060606 20060607 20060608 20060609 20060610 20060611
    20060612 20060613 20060614 20060615 20060616 20060617
    20060618 20060619 20060620 20060621 20060622 20060623
    20060624 20060625 20060626 20060627 20060628 20060629
    20060630 20060701 20060702"
# loop for each species
for sp in $SPECIES; do
# loop for all days in this scenario
  for year in  $YYYYMMDD; do
    FILE=camx5401pr_cb05.$year.dfw8h2.bc06_06jun.reg2.2006ep0ext_eta_5soil_sfcfddats_tkekv200.${RUN}.avrg.grd03
    if [ -f $FILE ]
    then
      MMDD=`echo $year | cut -c 5-8`
      echo "Grabbing $sp Data at EML for $year"
      echo "${FILE} is being processed"
# call pave
      pave \
      -f ${DATA_DIR}/${FILE} \
      -subdomain 24 46 24 46 \
      -s "${sp}a*1000" \
      -save2ascii ./${MMDD}.${sp}.dat \
      -exit
    else
      echo "File $FILE not found..."
    fi
  done
done
#-------------------------Patch all *.dat files together---------------*
# define io species/files for program tscln
#
FIN_TYPE="dat"
FOUT_TYPE="csv"
# define species
SPEC1="HCL"
SPEC2="CLV1"
# define i/o file types corresponding to extracted species
FIN1="*.$SPEC1.$FIN_TYPE"
FOUT1="*$FIN1.$FOUT_TYPE*"
FCOMP1="$SPEC1.$RUN.$FOUT_TYPE"
FIN2="*.$SPEC2.$FIN_TYPE"
FOUT2="*$FIN2.$FOUT_TYPE*"
FCOMP2="$SPEC2.$RUN.$FOUT_TYPE"
# input/out directory and program directories
OUTDIR="/corral/work/cfaxon1/camx/output/dfw8h2.tx.prm/eml_time_series/$RUN/"
HEADER="/corral/work/cfaxon1/data_processing/tscln/header.txt"
PROG="/corral/work/cfaxon1/data_processing/tscln/tscln"
# make sure output directory exists
if [ ! -d $OUTDIR ]
  then
    mkdir $OUTDIR
fi

# Patch all files together
for file in $FIN1; do
  OUT="$OUTDIR/$FCOMP1"
  if [ -f $file ]
  then
    echo Next input file: $file
    echo "$file" | $PROG
  else
    echo Input file $file does not exist.
  fi
done
# Patch all files together
cat $HEADER $FOUT1 > $OUT

for file in $FIN2; do
  OUT="$OUTDIR/$FCOMP2"
  if [ -f $file ]
  then
    echo Next input file: $file
    echo "$file" | $PROG
  else
    echo "Input file $file does not exist."
    echo 'input file does not exist.'
  fi
done
# Patch all files together
cat $HEADER $FOUT2 > $OUT
# Move all .csv files into output directory
mv *.$FOUT_TYPE $OUTDIR
# make subdir for dumped time series
JUNKDIR="./extracted_time_series"
if [ ! -d $JUNKDIR ]
  then
  mkdir $JUNKDIR
fi
mv *.$FIN_TYPE $JUNKDIR

The script dumps time series data from a single grid cell in a gridded output file and then calls a program to
change the format of each and then to combine all the days into one csv file. I'm new to bash and I'm sure there are
some more efficient ways to write the script, so any constructive criticism in that regard would be appreciated.

But what I mean by "not working" is that when I try to run it in the background using the ./[scriptname]& or ./[scriptname] &, everything still runs on the terminal and crowds screen as if I had run it without the &. I had assumed running in the background meant that it would leave the command line free so that I could continue working while it ran. Am I incorrect in my understanding? Thanks in advance for any feedback.

superdog 11-16-2012 12:36 PM

I think you need to also redirect output. So use something like
Code:

command &>/dev/null
or
Code:

command &>/tmp/output.log
if you want to log any errors. Not sure how good a method this is, just what i do when running noisy gui commands in a terminal

cfaxon1 11-19-2012 11:56 AM

Thanks for the tip superdog. That seemed to work, except that I had to write it like:

Code:

command > ./log.txt &
with the & at the end. I was misunderstanding that the output would be redirected by running it in the background,
but it makes sense that you have to indicate where it should be redirected.


All times are GMT -5. The time now is 10:24 AM.