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
# 1  
Old 07-29-2010
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 write any output to my logfile Smilie
Here is my Shell script:
Code:
#!/bin/sh

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
while [ 1 -eq 1 ]
do
        `perl handler.pl >> log.txt`
        echo "Perl-Script crashed, restarting..." >> log.txt
done

I think it would write if I don't put the `` around `perl handler.pl >> log.txt`, but then I can't send any signals to my perl script :/

Can (maybe) someone help me? ^^' thanks in advance and sorry for my bad english Smilie
# 2  
Old 07-30-2010
doesn't anyone have an idea? Smilie
pleaaase I need the code within the next 3 days and I really don't know how to get it working (neither does google) Smilie
# 3  
Old 11-17-2010
just joined the forum. and this thread is very old as well.

but would like to make a comment if some searcher can get this helpful.

you can schedule your script in cron to run every minute
and in your perl script check at the starting if another instance of script is running.
if so ...quit. else continue..


dont tell me that you need a code as well for this.. its pretty simple and wont take more then 3 lines..
# 4  
Old 11-17-2010
Thank you for your reply but that wasn't my problem...
My problem was that I couldn't log anything Smilie
Quote:
The problem is that it won't write any output to my logfile Smilie
And I asked for help to finish my code, not for the code itself Smilie
And btw I don't see why cron should help in my case? I needed a script making something when I send a TERM signal, not a script making something at a specific date/time...
# 5  
Old 11-17-2010
okay, so you were actually looking for a graceful shutdown or termination.
or technically saying a interrupt handler..


you probably could use
$SIG{'INT'} = 'interrupt_handler';

sub 'interrupt_handler {
print("Script got a termination signal, shutting down...!\n");
exit; # or may be you want to continue ignoring the signal then dont use exit.
}


I hope that would help.
# 6  
Old 11-17-2010
Not really shutdown - more a sighandler.
I didn't wanted to end my script but to make something (f.e. write "hello" into a text file).
[edit]: yeah I used that in my script but there was a problem that my script wouldn't catch any signals when I'm logging it via the script I posted... I'm sorry it's a bit complicated to explain >_< ...
I'm sorry I can't test your solution because I don't have the script anymore >_>
But it's nice that you answered for anyone finding this when searching for it! Smilie
# 7  
Old 11-20-2010
Maybe this is partly about logging on a buffered fd/FILE* like stdout, not unbuffered stderr, and no periodic flush(), with lost output when the script hangs and has to be killed? Logging through a pipe chain has a similar latency problem with potential lost output. One workaround for infrequent logging is to log by
Code:
system("echo 'Message' >>log_file")

or

system("date '+%Y-%m-%d %H:%M:%S Message' >>log_file")

or equivalent calls in perl using ``.
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