3

I'm new to bash scripting. I have a CentOS server. I'd like to run a command, something very simple like :

service lfd status

and then email that output to my email. Can someone help me with this, please? ultimately this is what i wanted to do : create a bash script like lfd.sh, run that bash script every 1 hour. So when i run that bash script, it will run this command "service lfd status" and send me the output via email..

  • 2
    What part of this is giving you trouble? I assume you've searched for "linux send email" or something, right? Did you try anything? Is your server set up to send mail? – terdon Aug 23 '19 at 16:23
  • Hi @terdon thanks for the reply. Yes, i have sendmail. My server is up and running - it's a web server actually. Centos7 + Cloudlinux. I found this - stackoverflow.com/questions/19327129/… but i dont think thats what im looking for. I actually searched "ssh run a command and email the output" – KSmith08 Aug 23 '19 at 16:29
  • Why is ssh relevant? Please edit your question and give all these details. Are you just looking for ssh you@yourServer 'service lfd status | mail ksmith08@yourdomain.com? – terdon Aug 23 '19 at 16:49
  • Hi @tedon. I dont know why you're confused by this question. SSH IS relevant. - I login to SSH - run this command : service lfd status to check the lfd status right? I need a bash script which will automate this for me. i.e script.sh i will set a 1 hour cron and run this script. that script will execute that status command and email me the output..... is that clear? – KSmith08 Aug 24 '19 at 5:08
2

Based on the other answers and OP's comments to the other answers, I think I can suggest an answer.

1.You want to run a command an email the output of it to some email. You have several options for sending mail on Linux. You'll need to make sure you can actually send email before your command can do it. Use a program like mail or mailx.

sudo yum install mailx

For more instructions configuring the mailx program, follow the directions here - https://www.digitalocean.com/community/tutorials/how-to-send-e-mail-alerts-on-a-centos-vps-for-system-monitoring

You could also use python or any programming language, but I'll forego that for now for simplicity.

2.Once mailx is setup based on the instructions in digitalocean, you should now be able to send emails like this:

echo "Your message" | mail -s "Message Subject" email@address

If you get that email, perfect! We're ready for the next part. Create your file lfd.sh

#!/bin/bash

service lfd status | mail -s "LFD Status" youremail@address.com

That should be all it takes to get the output of service lfd status emailed to you. If you want to run that, make sure you make it executable by running

chmod +x ./lfd.sh

So that should send you an email, but we're not done yet. You also mentioned you wanted it to automatically send you an email every hour. This can be done with crontab.

Crontab/Cron is a very useful automatic task scheduler on Linux. You'll need to create a Cron job with your lfd.sh

Websites like this help me remember the syntax for a Cron job.

crontab -e
0 * * * * /home/username/lfd.sh

That should run the job once every hour, at the top of the hour, every day of the week, if every month. For more info on that, view the Cron link or Google Cron examples.

  • wow @Brandon Zappy thank you so much for taking time to explain everything. This is what i needed. Once again I really appreciate your detailed answer :) – KSmith08 Aug 24 '19 at 9:05
  • @KSmith08 glad to help! I definitely made some assumptions that you knew things about Linux already, like the | "pipe" command, so I apologise if you weren't aware of some of these things. Hopefully everything together made sense though! – Brandon Zappy Aug 24 '19 at 13:31
  • Hi @Brandon Zappy the bash script is working fine when i run it manually. but when i configure cron like this : crontab -e 0 * * * * /home/username/lfd.sh im getting an email output like this : /usr/src/lfd.sh: line 2: service: command not found Null message body; hope that's ok any idea how to fix this? – KSmith08 Aug 25 '19 at 9:17
  • this is the manual email output from that script : ● lfd.service - ConfigServer Firewall & Security - lfd Loaded: loaded (/usr/lib/systemd/system/lfd.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2019-08-25 00:00:02 +08; 17h ago Process: 821360 ExecStart=/usr/sbin/lfd (code=exited, status=0/SUCCESS) Main PID: 821371 (lfd - sleeping) CGroup: /system.slice/lfd.service └─821371 lfd - sleepin Aug 25 00:00:02 123.xxxx.net systemd[1]: Starting ConfigServer Firewall & Security - lfd... – KSmith08 Aug 25 '19 at 9:17
  • @KSmith08 hi. It seems like Cron can't find the lfd command. That's what we're being told by the error "/usr/src/lfd.sh line 2" error. So what I would do is maybe use a full path to the lfd command. Are you running the Cron job as the same user and not as a different user? You may have to make sure your paths are the same. Try typing whereis lfd. That should return a full path to the lfd command. Take that full path and replace the relative path with the full path in your bash script. That's where I'd start to solve the issue. – Brandon Zappy Aug 25 '19 at 15:13
2

Try this one:

ssh -t user@sshserver.com ' touch /tmp/servicestatus.txt ; service lfd status > /tmp/servicestatus.txt ; mutt -s "Service Status" email@address.com < /tmp/servicestatus.txt '

As of:

-t to open the pseudo-terminal

touch command to create the file, once Ssh's pseudo-terminal seems to do not create it.

Notice that """ differs from "`".

And yes, Ssh does works with if/then/else statements.

If you wish the error output instead of any output, use 2> in place of >, as service lfd status 2> /tmp/servicestatus.txt

  • 1
    Hi thank you for the reply. Do i need to add this to a bash script now? if you dont mind me asking why i need that ssh login part? ultimately this is what i wanted to do : create a bash script like lfd.sh, run that bash script every 1 hour. So when i run that bash script, it will run this command "service lfd status" and send me the output via email.. – KSmith08 Aug 24 '19 at 5:10
  • 1
    It was something which was working before, my mistake, it is a command the do Ping test over Ssh. You would use this Ssh command if one wish to do this test in a remote network. In your case, one may do it as follows: #!/bin/bash while true; do cp /dev/null /tmp/servicestatus.txt ; date > /tmp/servicestatus.txt ; service lfd status >> /tmp/servicestatus.txt ; mutt -s "Lfd Service Status!" email@address.com < /tmp/servicestatus.txt ; sleep 3600 ; done – Vasconcelos1914 Aug 24 '19 at 15:20
-1
service lfd status >output

cat output|mail -s "subject" emailid </dev/null
  • Hi do i need to add this to the bash script or? – KSmith08 Aug 24 '19 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.