Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:53:46+00:00 2026-05-28T01:53:46+00:00

I just wrote a perl script that is restarting a list of services on

  • 0

I just wrote a perl script that is restarting a list of services on a linux server. It’s intended to run as a cron job. when I execute the script though, I keep getting this error;

root@www:~/scripts# ./ws_restart.pl
  * Stopping web server apache2                                   [ OK ]
sh: Syntax error: "(" unexpected
  * Stopping MySQL database server mysqld                         [ OK ]
sh: Syntax error: "(" unexpected

The call that is being used to do this is;

system("/etc/init.d/apache2 stop");
system("/etc/init.d/mysql stop");

I can paste the entire script code if needed, but I figured that this is the source of the problem and just need to know how to stop it.

Any ideas?

Here’s the entire script;

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my $old_pids = {};
my $post_stop_ids = {};
my @services = qw/apache2 mysql solr/;
my $app_dir  = '/home/grip/apps/eventfinder';


# collect existing pids then kill services
foreach my $service (@services) {
  # gather up existing pids
  $old_pids->{$service} = [ get_pids_by_process($service) ];

  # issue stop command to each service
  set_service_state($service, 'stop');

  # attempt to regather same ids
  $post_stop_ids->{$service} = [ get_pids_by_process($service) ];

  # kill any rogue ids left over
  kill_rogue_procs($post_stop_ids->{$service});

  # give each kill time to finish
  sleep(5);
}


# attempt to restart killed services
foreach my $service (@services) {
  # issue start command to each service
  set_service_state($service, 'start');

  # Let's give each service enough time to crawl outta bed.
  # I know how much I hate waking up
  sleep(5);
}

# wait for it!...wait for it! :P
# Pad an extra 5 seconds to give solr enough time to come up before we reindex
sleep(5);

# start the reindexing process of solr
system("cd $app_dir ; RAILS_ENV=production rake reindex_active");

# call it a day...phew!
exit 0;

sub kill_rogue_procs {
  my @ids = shift;
  # check if we still have any rogue processes that failed to die
  # if so, kill them now.
  if(scalar @ids) {
    foreach my $pid (@ids) {
      system("kill $pid");
    }
  }
}


sub set_service_state {
  my ($proc, $state) = @_;

  if($proc eq 'apache2') {
    system("/etc/init.d/apache2 $state");
  } elsif($proc eq 'mysql') {
    system("/etc/init.d/mysql $state");
  } elsif($proc eq 'solr') {
    system("cd $app_dir ; RAILS_ENV=production rake sunspot:solr:$state");
  }
}


sub get_pids_by_process {
  my $proc = shift;

  my @proc_ids = ();

  open(PSAE, "/bin/ps -ae | grep $proc |") || die("Couldn't run command");

  while(<PSAE>) {
    push @proc_ids, $_ =~ /(\d{1,5})/;
  }

  close PSAE;

  return @proc_ids;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:53:47+00:00Added an answer on May 28, 2026 at 1:53 am

    Actually, I’d be more suspicious of what’s in @ids in kill_rogue_procs. It’s the result of a ps followed by a grep, so might have bogus values if ps doesn’t return any results or if the pid isn’t 5 digits long.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote a very simple perl tk script to read a huge text
I wrote a simple perl script with the WWW::Selenium module that interacts with the
I am working with Perl programs that write to an XML file by just
Just wrote a web service and try to return columns of a sharepoint list
I just wrote a PHP login script, and what I'm trying to accomplish is
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to
I have a quite simple perl script, that in one function does the following:
I am trying to insure that only one instance of a perl script can
Suppose I have a Perl script, namely mytest.pl. Can I run it by something
I'm trying to write a perl script that determines which users are currently logged

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.