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 8029261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:23:00+00:00 2026-06-05T00:23:00+00:00

In pseudo code I’m trying to do something like the following if myService is

  • 0

In pseudo code I’m trying to do something like the following

if myService is running
  restart myService
else
  start myService

How can I translate the above into a bash script or similar?

  • 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-06-05T00:23:02+00:00Added an answer on June 5, 2026 at 12:23 am

    The standard way is to use a PID file for storing the PID of the service. Then, you can use the PID stored in the PID file to see if the service is already running or not.

    Take a look at the various scripts under the /etc/init.d directory and see how they use the PID file. Also take a look under /var/run in most Linux systems to see where the PID files are stored.

    You can do something like this which is a generic way of handling this for all Bourne shell type shells:

    # Does the PID file exist?
    
    if [ -f "$PID_FILE" ]
    then
        # PID File does exist. Is that process still running?
        if ps -p `cat $PID_FILE` > /dev/null 2&1
        then
    
           # Process is running. Do a restart
           /etc/init.d/myService restart
           cat $! > $PID_FILE
        else
           # Process isn't' running. Do a start
           /etc/init.d/myService start
           cat $! > $PID_FILE
    else
       # No PID file to begin with, do a restart
       /etc/init.d/myService restart
       cat $! > $PID_FILE
    fi
    

    However, on Linux, you can take advantage of pgrep:

    if pgrep myService > /dev/null 2>&1
    then
        restart service
    else
        start service
    fi
    

    Note how you do not use any braces. The if statement operates on the exit status of the pgrep command. I’m outputting both STDOUT and STDERR to /dev/null because I don’t want to print them. I just want the exit status of the pgrep command itself.

    READ THE MANPAGE ON PGREP

    There are quite a few options. For example, you might want to use -x to prevent unintended matches, or you might have to use -f to match on the full command line used to start the service.

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

Sidebar

Related Questions

In pseudo-code, what I mean is, can you do something like this (similar to
Running the following (slightly pseudo)code produces the following results. Im shocked at how innacurate
Basically in pseudo code I'm looking for something like if (connected_to_any_database()) { // do
I am not sure whether the following pseudo-code can generate an uniformly random permutation
The following is pseudo code for Kruskal's Minimum Spanning Tree algorithm from our CS
Hi I have below pseudo code with throws an exception like this throw new
The basic pseudo code looks like this: void myFunction() { int size = 10;
Using the following pseudo-code: #define BUILD_PATH C:/MyBuild/ #define BUILD_NAME mydll.dll // Set build path
i have following pseudo code : void siftup(int n) pre condition n>0 && heap(1,n-1)
I was looking at Wikipedia's pseudo-code (and other webpages like sortvis.org and sorting-algorithm.com) on

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.