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

  • SEARCH
  • Home
  • 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 1086583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:49:54+00:00 2026-05-16T22:49:54+00:00

I have this user crontab (accessed via the command crontab -e): # m h

  • 0

I have this user crontab (accessed via the command crontab -e):

# m h  dom mon dow   command
*/3 * * * *          sh /home/FRAPS/Desktop/cronCheck.sh

The script cronCheck.sh looks like that:

#!/bin/sh
SERVICE='Script'

if ps ax | grep -v grep | grep -i "$SERVICE" > /dev/null
then
    echo "######## $SERVICE service running, everything is fine ##################\n" >> CronReport.txt
else
    echo "$SERVICE is not running. Launching it now\n" >> CronReport.txt
    perl Script.pl 
fi

When I launch the script (cronCheck.sh) from its own directory, it works like a charm, but when cron launches it, it always “# $SERVICE service running, everything is fine ###”
despite ‘Script’ is not running.

Thanks,

  • 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-16T22:49:54+00:00Added an answer on May 16, 2026 at 10:49 pm

    Here’s an even better way to write that conditional:

    services=$(ps -e -o comm | grep -cFi "$SERVICE")
    case "$services" in
        (0) 
            # restart service
        ;;
        (1)
            # everything is fine
        ;;
        (*)
            # more than one copy is running
        ;;
    esac
    

    By using ps -e -o comm you avoid having to do the silly grep -v grep thing, because only the actual process name appears in the ps output, not the arguments. And grep -cFi counts up the matches and gives you a number, so you don’t have to deal with the exit status of a pipeline.

    Also, as other posters have implied, you should lead off this script by setting the PATH variable.

    PATH=/bin:/usr/bin:/sbin:/usr/sbin
    export PATH
    

    You might or might not want to put /usr/local/bin at the beginning of that list, depending on your system. Don’t do it if you don’t need anything from there.

    Final piece of advice: When writing scripts that will execute without user supervision (such as cron jobs), it’s a good idea to put set -e at the beginning. That makes them exit unsuccessfully if any command fails.

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

Sidebar

Related Questions

I have a greasemonkey user script with this single line of code... window.close(); but
Cron installation is vixie-cron /etc/cron.daily/rmspam.cron #!/bin/bash /usr/bin/rm /home/user/Maildir/.SPAM/cur/*; I Have this simple bash script
I have a ClickOnce environment like this: \\Fileserver\ClickOnceApps\App1.application C:\Documents and Settings\user\Start Menu\Programs\publisher\app1.appref-ms My understanding
I have a view user control that can post form. This control can be
Hej, assuming I have a code that looks like this: List<User> userList = GetUserByName
I have an entry in my crontab that looks like this: 0 3 *
The scenario is this: I have some users on my site. A user has
I have created a Python script that I want to run daily via a
I have this javascript that pulls some data via an ajax request the method
I have this c# user control class: public partial class UserControls_JsTop : System.Web.UI.UserControl {

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.