Perl script logging via Shell script


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Perl script logging via Shell script
# 8  
Old 11-21-2010
From my understanding your perl script should run all the time and if its gets killed your shell script should start it.

If above understanding is right then, i have some comments

Code:
#!/bin/sh

# Try to find PID of thing which starts with perl , check the count 
# if count is greater than 0 then kill whatever is running... 

# Bit risky unless you are 100% sure anything running with perl is 
# your script, too generic.

pidcheck=`pidof perl | grep -c [0-9][0-9][0-9][0-9]`
while [ $pidcheck -ne 0 ]
do
        kill -9 `pidof perl`
done
echo "\n------------------------------\n" >> log.txt
date >> log.txt
echo "\n------------------------------\n" >> log.txt

# i am not sure why are you running this script 
# without checking if script is running or not .. if that logic in the perl script
# then really bad coding to start another process every time , there is not 
# even sleep of 1-2 sec 

while [ 1 -eq 1 ]
# if you want to run while loop in infinite loop 
# use "while :" 
do
        # in perl to execute system command you use backticks 
        # where as in shell script you run directly 
        # like perl handler.pl >> log.txt
        # to get date did you do `date >> log.txt` ? 
        `perl handler.pl >> log.txt`
        echo "Perl-Script crashed, restarting..." >> log.txt
done

Here are my script 1 shell wrapper & 1 perl script both works fine i dont have *nix terminal @ my place so its on cygwin ..it works there so shouldnt be a problem on *nix

Code:
# cat a.sh
#! /bin/bash
echo "------------------\n">> /tmp/log.txt
date >>/tmp/log.txt
echo "------------------\n">> /tmp/log.txt

while :
do
    echo "Running perl script again ..."
    perl a.pl >> /tmp/log.txt 2>&1
    echo "Running Perl script for fun ...\n" >> /tmp/log.txt
done

# cat a.pl
#! /bin/env perl
  print "Fun Perl Script ....\n" ;
  sleep 10 ;
exit ;

# bash a.sh
Running perl script again ...
Running perl script again ...
Running perl script again ...

# cat /tmp/log.txt
------------------\n
Sun Nov 21 13:29:01 IST 2010
------------------\n
Fun Perl Script ....
Running Perl script for fun ...\n
------------------\n
Sun Nov 21 13:37:32 IST 2010
------------------\n
Fun Perl Script ....
Running Perl script for fun ...\n
Fun Perl Script ....
Running Perl script for fun ...\n


Last edited by zedex; 11-21-2010 at 04:11 AM.. Reason: spelling mistakes corrected
# 9  
Old 11-21-2010
To the original poster - you are doing some strange stuff here.

Normally to make sure your perl script always ran, you would run it like this:
PHP Code:
while true; do perl myscript.pl ; echo restartingdone out.log 
Add a & to run it in the background.

The "pidof perl" is dangerous since you might have many perl scripts running on the system. You'd be better off with something like this in your perl script:
Code:
open(pidfile,">$0.pid") && print pidfile $$;close pidfile;

# 10  
Old 11-21-2010
Quote:
Originally Posted by otheus
To the original poster - you are doing some strange stuff here.

Normally to make sure your perl script always ran, you would run it like this:
PHP Code:
while true; do perl myscript.pl ; echo restartingdone out.log 
Add a & to run it in the background.

The "pidof perl" is dangerous since you might have many perl scripts running on the system. You'd be better off with something like this in your perl script:
Code:
open(pidfile,">$0.pid") && print pidfile $$;close pidfile;

Yeah I know I didn't knew much about perl and shell scripts thats why I had a weird way to solve it, but now I know much more about shell that's why I'm not using this script anymore.
And I don't think there where any other Perl scripts running in the background, since the system doesn't have any perl scripts to run and it's an embedded system...
# 11  
Old 11-24-2010
Quote:
Originally Posted by al0x
And I don't think there where any other Perl scripts running in the background, since the system doesn't have any perl scripts to run and it's an embedded system...
Be careful! Some system scripts rely on perl for various tasks, such as logrotation, file indexing, etc.
# 12  
Old 11-24-2010
You can fuser the app's log before attaching to it, to see if you are already running, without additional files and fuss. I am not much of a perl jocey, but maybe you can do the sam check inside PERL; in ksh, it might look like this:
Code:
 
ps -fp $(fuser $logfile 2>/dev/null) | grep -c my_app | read ct

---------- Post updated at 10:48 AM ---------- Previous update was at 10:31 AM ----------

The pid file is popular, but you still need to ps -fp | grep or the like to ensure the pid in use is your app.

A tcp socket listener makes a nice mutex. You could assign a thread to providing a simple html status report within the app.


Using 'fuser' or the PERL equivalent on the app log file has several advantages and challenges:
  1. You find any children still running. You might want to purge them and restart if the main app is not running.
  2. No extra pid file.
  3. subshells make the count go up.
  4. users browsing the log may look like the app or like children, especially as the log path often contains the app name. (Maybe capitalize part of the app name in the log name?)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Enable logging from within the shell script

Bash on Oracle Linux 6.3 I have a shell script whose output I want to redict to a log file. So, I can simply redirect the output as shown below. # cat myscript.sh #### I actually want some logging mechanism here which will redirect the output to a log file echo 'hello world' #... (3 Replies)
Discussion started by: John K
3 Replies

2. Shell Programming and Scripting

shell script for multiple logging

Hi All, I am preparing a script which executes following things: 1) Logs into 8 cluster one by one. 2) After logging into each cluster,it prints the cluster name & then exit from that cluster. 3) Then it logs to next cluster & peform the same task. Here is what i have written : for... (8 Replies)
Discussion started by: d8011
8 Replies

3. Shell Programming and Scripting

Executing scipts after logging into a remote host using shell script

Hi All, Sorry if it is a duplicate post. I have not got any reference about this anywhere. I looked at the posts described in SSH - Passing Unix login passwords through shell scripts - Linux / UNIX Forum and it helped me till the point to connect to the host and executing the basic commands.... (3 Replies)
Discussion started by: RSC1985
3 Replies

4. Shell Programming and Scripting

Perl script logging via Shell script

Hello I wrote a nice Perl script that is intended to find and copy some files when getting a TERM signal. Now I wanted to make a shell script that starts/restarts the Perl script if it crashes/ends because of errors and make a log of all output the Perl Script gives. The problem is that it won't... (1 Reply)
Discussion started by: al0x
1 Replies

5. Shell Programming and Scripting

Shell Script for Logging into the Website

Hi ALL, Is there any way, to login into a website using Shell/Perl command/script? I am struggling on this from quite sometime but with no luck. Can you guys help, please? My sole purpose is to login a website (Which requires Username and Password) and then extract some information from... (3 Replies)
Discussion started by: parshant_bvcoe
3 Replies

6. Shell Programming and Scripting

Logging into oracle or SQL from shell script

Hi, I have a shell script where I log on to sqlplus like this log() { sqlplus -s scott/tiger <<! select count(*) from EMP; ! } log Here I have hardcoded/used the username : scott and password : tiger directly to log on to SQLPLUS. If i have my log in information in my profile file... (2 Replies)
Discussion started by: manirsendhil
2 Replies

7. Shell Programming and Scripting

Logging OWB mapping execution in Shell script

Hi, I am executing a OWB mapping from a shell script like this $OWB_SQLPLUS MY_WAREHOUSE plsql MY_MAPPING "," "," I want to log this mapping execution process into a file. Please let me know if this will work: $OWB_SQLPLUS MY_WAREHOUSE plsql MY_MAPPING "," "," >> LOGFIL.log I will... (1 Reply)
Discussion started by: npn
1 Replies

8. Shell Programming and Scripting

Have a script running even with the shell logging out

Hi all, I wish to have a script running even if my session is disconnected. I've tried calling another session within it and using sudo to a different user, but it didn't work - as it was expected to do so :rolleyes: I guess I'll have to work with "nohup" command, right ? trying with the... (4 Replies)
Discussion started by: 435 Gavea
4 Replies

9. Shell Programming and Scripting

logging in Shell script

How to I write to a log file all the output that is displaying on the screen? with time stamp. thankx. (3 Replies)
Discussion started by: laila63
3 Replies

10. UNIX for Dummies Questions & Answers

shell script error logging

Hi, I am writing a shell script (ksh, solaris 5.8). Script is X.sh variables declared: LOGFILE=X.log the script is removing a file: /usr/bin/rm "$DUMP_FILE1".Z >> $LOGFILE If I kick off the script from the command line as follows: ./X.sh > X.con 2<& 1 (1 Reply)
Discussion started by: niamh
1 Replies
Login or Register to Ask a Question