Martin
Martin

Reputation: 321

How would you tail a log via a bash script without the script ending?

I have a simple script that takes a users input for a remote host address and then based on the selection that they make for which log they want to tail, the script will start tailing that log.

Example:

printf "\nEnter the customers Cloud URL:\n"
read -r customerURL

printf "\nWhich log do you want to tail?\n1. JLOG\n2. CLOG\n3. HLOG\n"
read -r whichlog

ssh "$customerURL"
tail -f /path/to/log

When doing this, the script runs but then once it is done SSH'ing into the host, it ends. Is what I am trying to do possible? Is there a better way to handle this?

Upvotes: 0

Views: 52

Answers (2)

John Kugelman
John Kugelman

Reputation: 361575

Combine the ssh and the tail so the tail runs on the remote host instead of the local one.

ssh "$customerURL" tail -f /path/to/log

Upvotes: 6

Nick Bull
Nick Bull

Reputation: 9866

Use a background process:

tail -f /path/to/log &

If ending your SSH session kills commands, use nohup:

nohup tail -f /path/to/log &

You can also send this via ssh:

ssh [email protected] nohup tail -f /path/to/log &

Upvotes: 2

Related Questions