View the live log in linux

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.log

over 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

5

1 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 0
  • touch /tmp/vpn.log: creates a 0-length file named "vpn.log" in /tmp if not existing, or updates /tmp/vpn.log's access and modification time if existing; this is done to ensure that tail -f [...] won't exit on error;
  • gnome-terminal -e 'bash -c "echo $$ > pid; tail -f /tmp/vpn.log"': spawns a gnome-terminal instance and spawns a bash instance inside the gnome-terminal instance, which outputs its PID to a file named "pid" and runs tail -f /tmp/vpn.log;
  • kill -15 "$(< pid)": sends a SIGTERM signal to the bash instance running inside the gnome-terminal instance;
  • rm pid: removes "pid";

screenshot

7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like