Jorge
Jorge

Reputation: 400

How can I monitor a bash script?

I am running a bash script that takes hours. I was wondering if there is way to monitor what is it doing? like what part of the script is currently running, how long did it take to run the whole script, if it crashes at what line of the script stopped working, etc. I just want to receive feedback from the script. Thanks!!!

Upvotes: 1

Views: 2077

Answers (2)

dpp
dpp

Reputation: 1758

from man page for bash,

set -x 
After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.

add these to the start of your script,

export PS4='+{${BASH_SOURCE}:$LINENO} '
set -x

Example,

#!/bin/bash          

export PS4='+{${BASH_SOURCE}:$LINENO} '
set -x

echo Hello World  

Result,

+{helloworld.sh:6} echo Hello World
Hello World

Upvotes: 3

Zulu
Zulu

Reputation: 9275

Make a status or log file. For example add this inside your script:

echo $(date) - Ok >> script.log

Or for a real monitoring you can use strace on linux for see system call, example:

$ while true ; do sleep 5 ; done &
[1] 27190
$ strace -p 27190

Upvotes: 1

Related Questions