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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:50:51+00:00 2026-06-05T12:50:51+00:00

I have a bash script like this: #!/bin/bash startsomeservice & echo $! > service.pid

  • 0

I have a bash script like this:

#!/bin/bash
startsomeservice &
echo $! > service.pid

while true; do
    # dosomething in repeat all the time here
    foo bar
    sleep 5
done

# cleanup stuff on abort here
rm tmpfiles
kill $(cat service.pid)

the problem of this script is, that i cant abort it. If i press ctrl+c i just go into the next loop…
Is it possible to run a script like this but to have it abortable?

  • 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-05T12:50:52+00:00Added an answer on June 5, 2026 at 12:50 pm

    Since you are executing the script with Bash, you can do the following:

    #!/bin/bash
    
    startsomeservice &
    echo $! > service.pid
    
    finish()
    {
        rm tmpfiles
        kill $(cat service.pid)
        exit
    }
    trap finish SIGINT
    
    while :; do
        foo bar
        sleep 5
    done
    

    Please note that this behaviour is Bash specific, if you run it with Dash, for instance, you will see two differences:

    1. You cannot capture SIGINT
    2. The interrupt signal will break the shell loop.

    Note also that you will break a shell loop with a single C-c when you execute the loop directly from an interactive prompt, even if you’re running Bash. See this detailed discussion about SIGINT handling from shells.

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

Sidebar

Related Questions

I have a script that looks like this #!/bin/bash function something() { echo hello
I have a bash script that looks like this: #!/bin/sh previousRelease=`git describe --tags --match
If I have a Bash script like: #!/bin/bash f() { # echo function name,
I have a bash script which calls another bash script, like so: #!/bin/bash echo
I have a bash script like: #!/bin/bash echo Hello world! How do I execute
If I have a BASH script, like this: #!/bin/bash cp file1.txt file2.txt Can I
I currently have a bash script. It looks like this: #!/bin/bash case $1 in
I have a number of shell scripts that all look like this: #!/bin/bash cd
I have a bash script file which starts with a function definition, like this:
On my Centos 6.2 I have this bash script: [le_me]$ cat get.nb #! /bin/bash

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.