Skip to content
Advertisement

Redirect parallel process bash script output to individual log file

I have a requirement, where i need to pass multiple arguments to the script to trigger parallel process for each argument. Now i need to capture each process output in the separate log file.

for arg in test_{01..05} ; do bash test.sh "$arg" & done

Above piece of code can only give parallel processing for the input arguments. I tried with exec > >(tee "/path/of/log/$arg_filedate +%Y%m%d%H.log") 2>&1 and it was able to create single log file name with just date with empty output. Can someone suggest whats going wrong here or if there is any best way other than using parallel package

Advertisement

Answer

Try:

data_part=$(date +%Y%m%d%H)
for arg in test_{01..05} ; do bash test.sh "$arg" > "/path/to/log/${arg}_${data_part}.log" & done

If i use “$arg_date +%Y%m%d%H.log” it is creating a file with just date without arg

Yes, because $arg_ is parsed as a variable name

arg_=blabla
echo "$arg_"     # will print blabla
echo "${arg_}"   # equal to the above

To separate _ from arg use braces "${arg}_" would expand variable arg and add string _.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement