Solved

Bash Script-Trigger email when server reboots

Posted on 2016-09-26
11
55 Views
Last Modified: 2016-09-29
Hi Team,

I would like to write a script which should trigger email when server reboots automatically.Please help me out here

Thanks
Vinayak
0
Comment
Question by:vinayakssh
  • 4
  • 3
  • 2
11 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 41815997
start seperate script, or include code in /etc/rc.local
0
 

Author Comment

by:vinayakssh
ID: 41816000
Please let me know the steps as i am not aware

Thanks
Vinayak
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41816010
add this to your /etc/rc.local file, then reboot, you'll see it will mail you:

MESSAGE="Linux machine booted/n"

echo $MESSAGE | mail -s "Linux machine booted" "your_email@address.com"

Open in new window


Obviously, only works if your mail system is properly set up.
Run rc.local a few times to test if it works, then reboot for one final test.
0
 

Author Comment

by:vinayakssh
ID: 41816243
Hi,

Thanks for your help it is working fine as excepted.I would like to know same thing can be scheduled from cron job as well instead of putting in rc.local file

Thanks
Vinayak
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:Kent W
ID: 41816329
Yes, it can
Crontab entry -

@reboot /path/to/mailscript.sh

You will need to move the commands out of the rc.local and place in a separate executable shell script (x permissions by the cron job running user)

If you are using Ubuntu, you may have to make the crontab entry as such:

@reboot root /path/to/mailscript.sh
0
 

Author Comment

by:vinayakssh
ID: 41821838
Thanks for your solution and it is working fine.

Please let me know if you have any link for learning the bash script for beginners

Thanks
Vinayak
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 41821906
If the solution was provided here, you should award points to the person(s) who helped you.
If you award yourself 0 points, it really means you did everything yourself, and the answer was NOT provided by anyone (which clearly isn't the case here)
0
 
LVL 12

Expert Comment

by:Kent W
ID: 41822176
I would also like to interject OP also asked, specifically, if this script could be ran via crontab on reboot, of which I gave the corresponding answer.
0
 

Author Closing Comment

by:vinayakssh
ID: 41822936
Given Solution worked fine

Thanks
Vinaayak
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now