Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove pipe on exit #1

Open
mshoe007 opened this issue May 4, 2020 · 0 comments
Open

remove pipe on exit #1

mshoe007 opened this issue May 4, 2020 · 0 comments

Comments

@mshoe007
Copy link

mshoe007 commented May 4, 2020

Thanks for the nice sample code of a simple bash -based 'server' that takes commands from a remote client.

When play_stdin_server.sh exits abnormally (control-C, for example), the /tmp/play_stdin_server pipe is left lying around.

Not a huge deal, but it would be cleaner if the script removed it. If I run the script, do a control-C and run it again, I get an error message as the script tries to create the already existing pipe.

Use the 'trap on EXIT' feature of bash to ensure a cleanup function is run no matter how the script exits - both normally and abnormally

Just before "#Create the pipe", add:

always_run_on_exit () {
   # Use -f so that there is no error if the pipe does not yet exist or was already removed
   rm -f $CONTROLLER_PIP_FILE
}

# No matter how the script exits, ensure pipe gets removed
trap always_run_on_exit EXIT

Then you can remove the 'rm $CONTROLLER_PIPE_FILE' from server_exit() too. Although there's no harm in leaving it there as the 'cleanup_server_fifo' function uses 'rm -f' which won't report an error when trying to remove something that's not there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant