LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 12-22-2009, 11:16 PM   #1
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Rep: Reputation: 15
Exclamation How to run perl script as service


Can someone help me to figure out how I can write a perl script that tails a log file (/var/log/asterisk/messages, for example) constantly looking for a particular string, and if it finds that string, it should email a specified email address?

This perl script would have to run as a service, not something called from a cron.

I'm not that familiar with perl to begin with, so the more help you can lend, the better ... and I'm not really sure how to make it run constantly (as a service).
 
Old 12-22-2009, 11:29 PM   #2
baltho
Member
 
Registered: Aug 2004
Location: Adelaide, South Australia
Distribution: Ubuntu (Natty)
Posts: 38

Rep: Reputation: 18
I assume you're doing this on Windows, yes?
If not, let me know: if so, wrap the script in a CMD file - a one-liner with "C:\Path\To\perl.exe yourscript.pl" in it, then have a look at http://support.microsoft.com/kb/251192 regarding roll-your-own services.
 
Old 12-22-2009, 11:56 PM   #3
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Original Poster
Rep: Reputation: 15
Not windows

Quote:
Originally Posted by baltho View Post
I assume you're doing this on Windows, yes?
If not, let me know: if so, wrap the script in a CMD file - a one-liner with "C:\Path\To\perl.exe yourscript.pl" in it, then have a look at http://support.microsoft.com/kb/251192 regarding roll-your-own services.
No, sorry. I didn't specify. This is on a linux box (Debian).
 
Old 12-23-2009, 12:35 AM   #4
saifkhan123
Member
 
Registered: Apr 2009
Distribution: Red Hat/CentOS
Posts: 108

Rep: Reputation: 19
well, writing a script for this purpose is not a big deal, you can put a command into the script with its full path, for example,

you can start your script as

Code:
#!/bin/bash
#
/usr/bin/tail -f /var/log/asterisk/messages
further if you want this script to run continuously as the machine starts-up, for example, create a new file as vi /myscripts/logs.sh, its contents are like this

Code:
#!/bin/bash
#chkconfig: 345 80 05
#description: Logs monitoring script
/usr/bin/tail -f /var/log/asterisk/messages
save this file, make it executable by changing its permission as
Code:
chmod 755 /myscripts/logs.sh
If you are using RedHat or CentOS, than you copy the script file in /etc/rc.d/init.d directory, and than write this command
Code:
$ /sbin/chkconfig --add logs.sh  
$ /sbin/chkconfig logs.sh on
now when you reboot the system, the script will start on its priorites, and runs continuously as a service on runlevel 3,4 and 5

for details of these directives above, check this link
http://www.linuxjournal.com/article/4445

Remember 1 thing, it was only an example, you have to do some work on this script, and if you are using Ubuntu, then there are some different command available for starting and stopping daemons.
 
Old 12-23-2009, 01:51 AM   #5
baltho
Member
 
Registered: Aug 2004
Location: Adelaide, South Australia
Distribution: Ubuntu (Natty)
Posts: 38

Rep: Reputation: 18
lol - got half-way through writing exactly that answer before I saw the win-logo, then I deleted the lot.... doh!

Well, almost exactly: chkconfig might not be installed by default on debian: you may need the update-rc.d command instead. It has a man page...

Last edited by baltho; 12-23-2009 at 02:05 AM.
 
Old 12-23-2009, 09:14 AM   #6
tdnnash25
Member
 
Registered: Apr 2009
Posts: 63

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by saifkhan123 View Post
well, writing a script for this purpose is not a big deal, you can put a command into the script with its full path, for example,

you can start your script as

Code:
#!/bin/bash
#
/usr/bin/tail -f /var/log/asterisk/messages
further if you want this script to run continuously as the machine starts-up, for example, create a new file as vi /myscripts/logs.sh, its contents are like this

Code:
#!/bin/bash
#chkconfig: 345 80 05
#description: Logs monitoring script
/usr/bin/tail -f /var/log/asterisk/messages
save this file, make it executable by changing its permission as
Code:
chmod 755 /myscripts/logs.sh
If you are using RedHat or CentOS, than you copy the script file in /etc/rc.d/init.d directory, and than write this command
Code:
$ /sbin/chkconfig --add logs.sh  
$ /sbin/chkconfig logs.sh on
now when you reboot the system, the script will start on its priorites, and runs continuously as a service on runlevel 3,4 and 5

for details of these directives above, check this link
http://www.linuxjournal.com/article/4445

Remember 1 thing, it was only an example, you have to do some work on this script, and if you are using Ubuntu, then there are some different command available for starting and stopping daemons.

Yeah, I knew that I could do something simple like that. But I am not sure how to write it so that it searches for a specific string in the log file (while tailing), and if it finds that string, it should email me.
 
Old 12-24-2009, 01:35 AM   #7
baltho
Member
 
Registered: Aug 2004
Location: Adelaide, South Australia
Distribution: Ubuntu (Natty)
Posts: 38

Rep: Reputation: 18
It'd be something like:

if tail /var/log/syslog | grep "SEARCH_STRING" ; then
<mail command line>
else
echo "Not found"
fi

You could pipe the grep output into a file for including in the email (grep "SEARCH_STRING" >some_temp_file), or if you don't need to see the actual output, do a grep -q "SEARCH_STRING", which suppresses the output and just returns the success/fail code needed by the if.

All bash scripting, by the way.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I run a script as a service NickJH Linux - Newbie 5 09-14-2009 01:56 PM
Set a Perl script as an unix service (start with system) onlymed Linux - Newbie 2 04-01-2009 02:59 PM
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 03:29 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 09:56 AM
run perl service on Linuxstartup shahidawan Programming 2 03-30-2005 11:57 AM


All times are GMT -5. The time now is 04:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration