Every time when we write a script to automate some task we create the log file to store the required output from the script.
Here I am showing you all how to create a log file with timestamp and server name with python and shell script on Linux servers. So that you don't need to check in all the logs files when you look for a log of particular server.
Python Script:
root@linuxserver:/root> cat logfile_timestamp.py
#!/usr/bin/python
import os, time
logfile = "logfile_" + os.uname()[1] + "_" + str(time.strftime("%Y%m%d_%H%M%S")) + ".log"
f = open(logfile,'w')
f.close()
Run the script:
root@linuxserver:/root> python logfile_timestamp.py
Check if the log file created with server name and timestamp.
root@linuxserver:/root> ls -ltr logfile*
-rw-r--r-- 1 root root 0 Jun 27 02:45 logfile_linuxserver_20180627_024509.log
Shell Script:
root@linuxserver:/root> cat create_logfile.sh
#/bin/bash
logfile=logfile_`uname -n`_`date +%Y%m%d_%H%M%S`.log
touch $logfile
Run the script:
root@linuxserver:/root> ./create_logfile.sh
Check if the log file created with server name and timestamp.
root@linuxserver:/root> ls -ltr logfile*
-rw-r--r-- 1 root root 0 Jun 27 02:41 logfile_linuxserver_20180627_024142.log