-1

I am running the following command:

nohup myscript.sh &> file.log &

The process takes very long time to complete. The nohup will collect the output and error and write it into the file.log above.

myscript.sh will activate scripts in the background process as shown below:

# myscript.sh starts here
#!/bin/bash

status_bar(){
printf "["
While kill -0 "$1" 2>/dev/null; do
    printf "-"
    sleep 1
done
printf "] done!"
}

pid=()
for i "${array[@]}"; do
     echo starting process for "$i"
     script_A.sh "$i" &
     status_bar $!   # I need to print this funtion output in the terminal while running with nohup command
     pid="$!"
done


for id in ${pid[*]}; do
    wait $id
done

echo print its done

I need some of the lines from myscript.sh and/or script_A.sh to get written on the log file as well as to be printed on the console. I tried different approaches from the following links, but nothing seems to work. How can I do that?

redirect COPY of stdout to log file from within bash script itself

How to redirect output of an entire shell script within the script itself?

Redirect to stdout in bash

using nohup -> output to file and console

nohup myscript.sh &> file.log | tail -f file.log & 

The command above works but it prints all the lines and fully populates the terminal.

I need only specific lines within myscript.sh and/or script_A.sh to printed on the console so that user don't need to always open the log file for the status. (Currently, status_bar $! output is printing in the nohup log file but not in the terminal. I need to print this funtion status_bar $! output in the terminal while running with nohup command). Thanks in Advance.

Ahkam
  • 1
  • 1
  • Does this answer your question? [How to 'grep' a continuous stream?](https://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream) – Nic3500 Mar 03 '23 at 15:49
  • @Nic3500 I am looking for the possibility of redirecting the messages by making changes within the myscript.sh rather than grepping or tailing log file generated by the nohup command. – Ahkam Mar 05 '23 at 16:16
  • If so, then do you need only the relevant output lines to be written to the log in the first place (in which this is "how to grep the output stream from a script") or do you only want to extract those to the terminal (in which case, the duplicate nominated by Nic3500)? – tripleee Mar 06 '23 at 05:47
  • @tripleee I want certain things to be printed on the terminal but not based on user input (like using grep or tail command on the nohup's log file). I am looking for some trick which is possible I can do to print certain lines of log file also on the terminal. So, the user who runs the script can know the status without opening (grepping) the log file . I have also updated the script in the question for making it clear what I am look for... – Ahkam Mar 07 '23 at 08:27
  • A common solution to this sort of problem is to output messages to the system log with different priorities. Then each consumer can decide which priorities they are interested in, and easily grep for only WARNING or ERROR messages, for example. You can implement this in your own output, too, of course. Conversely, many utilities let you set a verbosity when you start them, and will only output messages at the selected priorities. The customary levels are DEBUG, INFO, WARNING, and ERROR (with perhaps a higher still level for critical failures, which your userspace program ideally cannot cause). – tripleee Mar 07 '23 at 14:16

1 Answers1

0

Here is an approach which would be fully customizable to each of the jobs being run.

Suggested adaptation of your script:

#!/bin/bash

pid=""
for i in "${array[@]}"
do
     echo starting process for "$i"
     script_A.sh "$i" &
     pid="$!"

     ### ActionForProcess_${i} must know names of specific output files
     ###    from "script_A.sh ${i}" to parse for meaningful messages. 
     nohup ( wait ${pid} ; "actionForProcess_${i}" ) &
done

echo print its done
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • You still need proper quoting around `"${array[@]}"` – tripleee Mar 06 '23 at 05:45
  • @Eric Marceau Thanks for your suggestion. I tried your adaptation but it is not working in my case. I will update my script in the question for more clear objective of my question. – Ahkam Mar 07 '23 at 07:58