I want to See the Live log from the Log file which is being used within the Script.
i have script which makes VPN connection to CISCO VPN Server and then COpy the files to the Remote Location. for having a list of files which was sent , within my VPN Copy Script , i am also making the log files and the LOG file location is in the tmp directory. my VPN copy script is working really fine without any issue.
but now what i am trying to do is i run this command
watch tail -n 15 /tmp/vpn.log
tail -f /tmp/vpn.logover the terminal and wants to see the Live log of the log files , and in this log file i will see the Information about the file transfer to remote location. but my problem is when i run
watch tail -n 15 /tmp/vpn.log
tail -f /tmp/vpn.log these commands and then run my Script then my files doestn transfer to the remote location and when i am not running tail command over the log files then my VPN copy script works without any issue.
so basically want that i can view the live log and my vpn copy script also can works.
can someone please help me ?
thanks
51 Answer
Running watch tail -n 15 /tmp/vpn.log / tail -f /tmp/vpn.log in the script stops its execution, because the when executing watch tail -n 15 /tmp/vpn.log / tail -f /tmp/vpn.log the shell is busy running watch tail -n 15 /tmp/vpn.log / tail -f /tmp/vpn.log themselves;
Generally a solution could be running the process as a background job or running the process separately from the current shell by any mean, however since you need to see the output of the process in question, a nice way to do this would be to run it in a new gnome-terminal instance:
#!/bin/bash
touch /tmp/vpn.log
gnome-terminal -e 'bash -c "echo $$ > pid; tail -f /tmp/vpn.log"'
echo "Starting to output to /tmp/vpn.log"
echo line1 >> /tmp/vpn.log
sleep 1
echo line2 >> /tmp/vpn.log
sleep 1
echo line3 >> /tmp/vpn.log
kill -15 "$(< pid)"
rm pid
exit 0touch /tmp/vpn.log: creates a 0-length file named "vpn.log" in/tmpif not existing, or updates/tmp/vpn.log's access and modification time if existing; this is done to ensure thattail -f [...]won't exit on error;gnome-terminal -e 'bash -c "echo $$ > pid; tail -f /tmp/vpn.log"': spawns agnome-terminalinstance and spawns abashinstance inside thegnome-terminalinstance, which outputs its PID to a file named "pid" and runstail -f /tmp/vpn.log;kill -15 "$(< pid)": sends a SIGTERM signal to thebashinstance running inside thegnome-terminalinstance;rm pid: removes "pid";