Sponsored Content
Top Forums Shell Programming and Scripting perl process loop isn't running Post 302349435 by kwick6 on Tuesday 1st of September 2009 04:28:49 AM
Old 09-01-2009
perl process loop isn't running

I'm trying to figure out why the perl process we have running in a loop isn't working.

Basically its setup to read our queue from Amazon SQS with the results getting inserted into the db.

We are using EC2 for video transcoding and once the conversion takes place our web server hosted outside of Amazon has a perl process that reads SQS.

I'm not a dev but I think I can find the problem if given some suggestions on how to test. I was trying to locate the "aws-daemon" log but wasn't able to find it in /var/log. I see that first line below states to exit if theres an INT flag, but I'm not sure what that means, and if it did set that flag does it mean theres a problem that is still existing somewhere preventing this process from working.

I've rebooted the server, and launched a new ec2 instance but other than that I don't know enough on how to kill or restart a process or whether thats the way to do it. I know there is stuff sitting in the queue waiting for this process to pull it and insert it into the db. Any help would be greatly appreciated.

Code:
#!/usr/bin/perl -w
use Error qw(:try);
use MIME::Base64;
use FindBin qw($Bin);
use POSIX qw(setsid setuid);
use Config::Auto;
use Proc::Daemon;
use Log::Log4perl qw(:easy);
use Platform;
use Platform::Queue;
use Platform::Video;
use strict;

# when we get a INT signal, set the exit flag
$SIG{INT} = sub { $::exit = 1 };

# setup logging
Log::Log4perl->easy_init($TRACE);

my ($log, $conf, $video, $sqs, $queue);

$log = Log::Log4perl->get_logger('aws-daemon');
$conf = Config::Auto::parse($Bin . "/aws.conf", format => "colon");
$video = Platform->new();
$sqs = Platform::Queue->new({
  aws_access_key_id     => $conf->{'aws_id'},
  aws_secret_access_key => $conf->{'aws_secret'} });

# fork into the background
# do this first because our process id will change
Proc::Daemon::Init;
$log->trace('calling daemonize()');
&daemonize();

# our infinite loop
while(1) {
  $log->trace('running...');
  exit if $::exit;
  check_queues($conf, $sqs, $video);
  $log->trace('sleeping for two minutes...');
  sleep(120);
  exit if $::exit;
  $log->trace('still running...');
}

sub check_queues {
  $log->trace('check_queues');
  my ($conf, $sqs, $video) = @_;

  try {
    $video->read_result_queue();
  } catch Error with {
    my $ex = shift;
    $log->trace('Exception in check_queues: ' . $ex);
  } finally {
  }
}

sub daemonize {
    chdir '/'                 or die "Can't chdir to /: $!";
    open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
    open STDOUT, '>>/tmp/awsd' or die "Can't write to /dev/null: $!";
    open STDERR, '>>/tmp/awsd' or die "Can't write to /dev/null: $!";
    defined(my $pid = fork)   or die "Can't fork: $!";
    exit if $pid;
    setsid                    or die "Can't start a new session: $!";
    umask 0;
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl problem - why isn't 'die' being called?

last week i started learning perl, so have limited skill and knowledge. why isn't 'die' being called and the script exiting before the 'directory created' line? if (! -d "$logdir") { system "mkdir -p $logdir" || die print "\nERROR: release log directory creation failed - $logdir: $!\n";... (4 Replies)
Discussion started by: mjays
4 Replies

2. UNIX for Dummies Questions & Answers

perl scripting for checking if a process is running

Hi All, I am new to perl and have been trying to write a short script to check a process.Though i havent reached to the stage where i can match the output. I am trying to pass a variable x with value /opt/RGw/csbp-base/CSBP_BAT.01.00.05/csbp_BAT.01.00.05.jar and then pass another variable... (2 Replies)
Discussion started by: pistachio
2 Replies

3. Shell Programming and Scripting

infinite loop to check process is running

Hi, I got a simple script working to check if a process is running and then email if it is not running anymore. My scenario is that I need to make sure the process is always running so instead of running the below script via cron I think it is better to a have a looping script to check... (12 Replies)
Discussion started by: yabai
12 Replies

4. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

5. Shell Programming and Scripting

loop when process running

Hi Gurus, Could you please help me to create a shell script that will be started by a cron job once every night at 24.00 h (that should bee easy:)) The shell script should control every 30 seconds the name of a process, and when the process doesn't run anymore it should execute a few further... (12 Replies)
Discussion started by: blackwire
12 Replies

6. Linux

X Window isn't running

X Window isn't working. When I ran startx I got an error message as shown in file attachment xwndow.png Accordingly, I removed /tmp/.X0-lock as reflecting in the attchment. Then again I ran the command and 2 more commands (shown in the attachment xwndow1.png) but they failed too. Any... (2 Replies)
Discussion started by: ravisingh
2 Replies

7. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

8. BSD

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (0 Replies)
Discussion started by: naveeng
0 Replies

9. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (6 Replies)
Discussion started by: naveeng
6 Replies

10. Shell Programming and Scripting

Command to get exact tomcat process I am running ignoring other java process

Team, I have multiple batchjobs running in VM, if I do ps -ef |grep java or tomcat I am getting multiple process list. How do I get my exact tomcat process running and that is unique? via shell script? (4 Replies)
Discussion started by: Ghanshyam Ratho
4 Replies
Daemon(3pm)						User Contributed Perl Documentation					       Daemon(3pm)

NAME
App::Daemon - Start an Application as a Daemon SYNOPSIS
# Program: use App::Daemon qw( daemonize ); daemonize(); do_something_useful(); # your application # Then, in the shell: start application, # which returns immediately, but continues # to run do_something_useful() in the background $ app start $ # stop application $ app stop # start app in foreground (for testing) $ app -X # show if app is currently running $ app status DESCRIPTION
"App::Daemon" helps running an application as a daemon. The idea is that you prepend your script with the use App::Daemon qw( daemonize ); daemonize(); and 'daemonize' it that way. That means, that if you write use App::Daemon qw( daemonize ); daemonize(); sleep(10); you'll get a script that, when called from the command line, returns immediatly, but continues to run as a daemon for 10 seconds. Along with the common features offered by similar modules on CPAN, it o supports logging with Log4perl: In background mode, it logs to a logfile. In foreground mode, log messages go directly to the screen. o detects if another instance is already running and ends itself automatically in this case. o shows with the 'status' command if an instance is already running and which PID it has: ./my-app status Pid file: ./tt.pid Pid in file: 14914 Running: no Name match: 0 Actions "App::Daemon" recognizes three different actions: my-app start will start up the daemon. "start" itself is optional, as this is the default action, $ ./my-app $ will also run the 'start' action. By default, it will create a pid file and a log file in the current directory (named "my-app.pid" and "my-app.log". To change these locations, see the "-l" and "-p" options. If the -X option is given, the program is running in foreground mode for testing purposes: $ ./my-app -X ... stop will find the daemon's PID in the pidfile and send it a SIGTERM signal. It will verify $App::Daemon::kill_retries times if the process is still alive, with 1-second sleeps in between. To have App::Daemon send a different signal than SIGTERM (e.g., SIGINT), set use POSIX; $App::Daemon::kill_sig = SIGINT; Note that his requires the numerial value (SIGINT via POSIX.pm), not a string like "SIGINT". status will print out diagnostics on what the status of the daemon is. Typically, the output looks like this: Pid file: ./tt.pid Pid in file: 15562 Running: yes Name match: 1 /usr/local/bin/perl -w test.pl This indicates that the pidfile says that the daemon has PID 15562 and that a process with this PID is actually running at this moment. Also, a name grep on the process name in the process table results in 1 match, according to the output above. Note that the name match is unreliable, as it just looks for a command line that looks approximately like the script itself. So if the script is "test.pl", it will match lines like "perl -w test.pl" or "perl test.pl start", but unfortunately also lines like "vi test.pl". If the process is no longer running, the status output might look like this instead: Pid file: ./tt.pid Pid in file: 14914 Running: no Name match: 0 The status commands exit code complies with http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html and returns 0: if the process is up and running 1: the process is dead but the pid file still exists 3: the process is not running These constants are defined within App::Daemon to help writing test scripts: use constant LSB_OK => 0; use constant LSB_DEAD_PID_EXISTS => 1; use constant LSB_DEAD_LOCK_EXISTS => 2; use constant LSB_NOT_RUNNING => 3; use constant LSB_UNKNOWN => 4; use constant ALREADY_RUNNING => 150; Command Line Options -X Foreground mode. Log messages go to the screen. -l logfile Logfile to send Log4perl messages to in background mode. Defaults to "./[appname].log". Note that having a logfile in the current directory doesn't make sense except for testing environments, make sure to set this to somewhere within "/var/log" for production use. -u as_user User to run as if started as root. Defaults to 'nobody'. -l4p l4p.conf Path to Log4perl configuration file. Note that in this case the -v option will be ignored. -p pidfile Where to save the pid of the started process. Defaults to "./[appname].pid". Note that having a pidfile in the current directory doesn't make sense except for testing environments, make sure to set this to somewhere within "/var/run" for production use. -v Increase default Log4perl verbosity from $INFO to $DEBUG. Note that this option will be ignored if Log4perl is initialized independently or if a user-provided Log4perl configuration file is used. Setting Parameters Instead of setting paramteters like the logfile, the pidfile etc. from the command line, you can directly manipulate App::Daemon's global variables: use App::Daemon qw(daemonize); $App::Daemon::logfile = "mylog.log"; $App::Daemon::pidfile = "mypid.log"; $App::Daemon::l4p_conf = "myconf.l4p"; $App::Daemon::background = 1; $App::Daemon::as_user = "nobody"; use Log::Log4perl qw(:levels); $App::Daemon::loglevel = $DEBUG; daemonize(); Application-specific command line options If an application needs additional command line options, it can use whatever is not yet taken by App::Daemon, as described previously in the "Command Line Options" section. However, it needs to make sure to remove these additional options before calling daemonize(), or App::Daemon will complain. To do this, create an options hash %opts and store application-specific options in there while removing them from @ARGV: my %opts = (); for my $opt (qw(-k -P -U)) { my $v = App::Daemon::find_option( $opt, 1 ); $opts{ $opt } = $v if defined $v; } After this, options "-k", "-P", and "-U" will have disappeared from @ARGV and can be checked in $opts{k}, $opts{P}, and $opts{U}. Gotchas If the process is started as root but later drops permissions to a non-priviledged user for security purposes, it's important that logfiles are created with correct permissions. If they're created as root when the program starts, the non-priviledged user won't be able to write to them later (unless they're world- writable which is also undesirable because of security concerns). The best strategy to handle this case is to specify the non-priviledged user as the owner of the logfile in the Log4perl configuration: log4perl.logger = DEBUG, FileApp log4perl.appender.FileApp = Log::Log4perl::Appender::File log4perl.appender.FileApp.filename = /var/log/foo-app.log log4perl.appender.FileApp.owner = nobody log4perl.appender.FileApp.layout = PatternLayout log4perl.appender.FileApp.layout.ConversionPattern = %d %m%n This way, the process starts up as root, creates the logfile if it doesn't exist yet, and changes its owner to 'nobody'. Later, when the process assumes the identity of the user 'nobody', it will continue to write to the logfile without permission problems. Detach only If you want to create a daemon without the fancy command line parsing and PID file checking functions, use use App::Daemon qw(detach); detach(); # ... some code here This will fork a child, terminate the parent and detach the child from the terminal. Issued from the command line, the program above will continue to run the code following the detach() call but return to the shell prompt immediately. AUTHOR
2008, Mike Schilli <cpan@perlmeister.com> LICENSE
Copyright 2008-2012 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-02-24 Daemon(3pm)
All times are GMT -4. The time now is 12:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy