Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Script Explanation

Getting all jobs from server


log_file=$(bjobs -u all)
By executing this command we can get all jobs in the server which is assigned to log_file
variable.

Creating csv file


csv_file="output.csv"
By executing above command we are creating csv file to which all jobs will be converted we get
from server.

Converting log file to csv file


awk '{print}' "$log_file"| tr '\t' ',' > "$csv_file"
In this line we are reading log file and converting that to csv file.

Providing email details


# Email details
recipient_email="asadghalib6@gmail.com"
sender_email="asadrehmanbangash2@gmail.com"
email_subject="Log File CSV Report"
email_body="Attached is the CSV report generated from the log file."
Within this code we are providing email details to which generated csv file will be sent. In this
code we are providing recipient email, sender email, subject and body. We can add more things
per our requirements.
Sending email
# Check if the CSV file was created successfully
if [ -s "$csv_file" ]; then
echo "CSV file created successfully."

# Send an email with the CSV file attached


{
echo "To: $recipient_email"
echo "From: $sender_email"
echo "Subject: $email_subject"
echo "$email_body"

} | mutt -s "$email_subject" -a "$csv_file" -- "$recipient_email"

echo "Email sent successfully."


else
echo "CSV file creation failed or is empty."
fi

Within this if condition we are checking that if the CSV file is generated then send it in email.
if [ -s "$csv_file" ]; then
this is start of if condition where if it is successful then it will execute this line
echo "CSV file created successfully."

and will proceed with email sending .

mutt -s "$email_subject" -a "$csv_file" -- "$recipient_email"

we are using mutt keyword to send email. We can use mail keyword or ssmtp as well
to send an email.

You might also like