user3526312
user3526312

Reputation: 75

Can I store ssh command output inside a variable(date/unique alphanumeric) folder

I'm creating a bash script in which I will run list of ssh command and output it inside a folder as a text files. What I'm doing is creating a list of command logs and saving it in a pre defined folder.

#!/bin/bash
tail -200 /opt/cpanel/ea-php74/root/var/log/php-fpm/error.log > /root/SERVERLOGS/php-fpm-logs.txt
tail -500 /var/log/apache2/error_log > /root/SERVERLOGS/apache-logs.txt
cd /var/lib/redis/ && ls -lsh > /root/SERVERLOGS/redisfilesize.txt
tail -500 /var/log/redis/redis.log > /root/SERVERLOGS/redis-logs.txt
df -h > /root/SERVERLOGS/harddisk.txt
free -m > /root/SERVERLOGS/RAM.txt
top -n 1 -b > /root/SERVERLOGS/top-output.txt

Whenever my server application is down I'll run it and get the output inside the SERVERLOGS folder. This works okay but if I run this command multiple times then the updated files is saved in the SERVERLOGS folder.

What I want to do is that whenever the application is down and I run my script it should create one unique folder and save inside it instead of SERVERLOGS folder. I can create unique folder using

mkdir /root/SERVERLOGS/$(date +"%d-%m-%Y-%h-%m")

But I don't know how to put the other commands inside the folder created by it...

Thanks for any input in this.

Upvotes: 0

Views: 51

Answers (1)

Barmar
Barmar

Reputation: 780889

Assign a variable at the beginning of the script with that directory name.

outputdir="/root/SERVERLOGS/$(date +"%d-%m-%Y-%h-%m")"
mkdir "$outputdir"

and then use filenames like "$outputdir/harddisk.txt" and "$outputdir/top-output.txt" in the rest of the script.

Upvotes: 1

Related Questions