Questions tagged [shell-trap]

18 questions
12
votes
3 answers

Exit after trap fires

Take this script #!/bin/sh fd () { echo Hello world exit } trap fd EXIT INT for g in {1..5} do echo foo sleep 1 done I would like fd to fire once, either from Control-C or if the script exits normally. However if you hit Control-C it…
Zombo
  • 1
  • 62
  • 391
  • 407
8
votes
1 answer

Is trap EXIT required to execute in case of SIGINT or SIGTERM received?

I have a simple script trap 'echo exit' EXIT while true; do sleep 1; done and it behaves differently in different shells: $ bash tst.sh ^Cexit $ dash tst.sh ^C $ zsh tst.sh ^C $ sh tst.sh ^Cexit So I'm not sure about how it should operate and…
Ixanezis
  • 1,631
  • 13
  • 20
8
votes
1 answer

Simultaneously watch for signals and process exit in Bourne shell

I have a Bourne shell (/bin/sh) script (for portability) that wants to monitor another program. It should start the other program, then wait for it to exit. When the second program exits, it does some final work and exits itself. The catch is…
Alan De Smet
  • 1,699
  • 1
  • 16
  • 20
4
votes
2 answers

Restore traps without a temp file

Without arguments trap prints the currently set commands for all traps. However, a subshell does not inherit traps, so the canonical example for saving and restoring traps fails in bash: save_traps=$(trap) ... eval "$save_traps" The trap on the…
William Pursell
  • 204,365
  • 48
  • 270
  • 300
4
votes
1 answer

why is this simple bash trap failing

I'm still pretty new to bash scripting, and I'm having a hard time figuring out why this simple trap is not working as expected. Goal - create an optional waiting period that can be skipped by pressing CTRL+C. Expected result of pressing CTRL+C -…
3
votes
3 answers

trapping shell exit code

I am working on a shell script, and want to handle various exit codes that I might come across. To try things out, I am using this script: #!/bin/sh echo "Starting" trap "echo \"first one\"; echo \"second one\"; " 1 exit 1; I suppose I am missing…
jpou
  • 1,935
  • 2
  • 21
  • 30
2
votes
3 answers

Multiple actions in trap in ksh

After sudo su -, I want to have both the username and timestamp in the k-shell history list. I have the command line: trap 'who am i|cut -d" " -f1 |tr "\n" " " && date|read -s' debug With this command, I expect something like: UserName Tue Oct 13…
Heinz
  • 913
  • 4
  • 12
  • 22
1
vote
2 answers

Idiomatic way to exit ksh while loop

I've the following 5 second timer which prints an asterisk for each second. timer () { i=1 trap 'i=5' INT while [[ $i -le 5 ]]; do sleep 1 printf "*" ((i+=1)) done } Somehow the trap chunk seems a little hackish and I wonder if…
amorphousone
  • 65
  • 1
  • 4
1
vote
1 answer

Ending Timestamp not printing on Shell Script: Using trap

I have a shell script I use for deployments. Since I want to capture the output of the entire process, I've wrapped it in a subshell and tail that out: #! /usr/bin/env ksh #…
David W.
  • 105,218
  • 39
  • 216
  • 337
1
vote
2 answers

custom trap signal in ksh

Is there a way for me to implement custom fake signals in ksh? Currently am capturing the ERR signal and exiting. However, due to a change, there are calls that may not return success, however that is a valid condition. In such case, I want to make…
Kiran
  • 993
  • 2
  • 9
  • 14
1
vote
1 answer

Why doesn't script call the cleanup and kill the script after 5s, and I also want to get where it stoppped

The script is designed to kill itself after 5s. But it doesn't kill the script after 5s, and does not call the cleanup. The important thing is that I want to get the value of i when it stopped. #!/bin/sh trap "cleanup" TERM #call the cleanup when…
user1334609
  • 381
  • 4
  • 12
1
vote
1 answer

Trap syntax issue in bash

I intend to use trap to execute some clean up code in case of a failure. I have the following code, but it seems to be have some syntactical issues. #!/bin/bash set -e function handle_error { umount /mnt/chroot losetup -d $LOOP_DEV1…
The Governor
  • 1,152
  • 1
  • 12
  • 28
0
votes
1 answer

In bash: processing every command line without using the debug trap?

I have a complicated mechanism built into my bash environment that requires the execution of a couple scripts when the prompt is generated, but also when the user hits enter to begin processing a command. I'll give an oversimplified description: The…
Sniggerfardimungus
  • 11,583
  • 10
  • 52
  • 97
0
votes
2 answers

How do I trap EXIT in a solaris 11 shell script?

man signal.h indicates there's no SIGEXIT in Solaris 11. How can I trap it in my shell scripts? Or how can I simulate the old behavior of traping SIGEXT?
0
votes
2 answers

shell script process termination issue

/bin/sh -version GNU sh, version 1.14.7(1) exitfn () { # Resore signal handling for SIGINT echo "exiting with trap" >> /tmp/logfile rm -f /var/run/lockfile.pid # Growl at user, exit # then exit…
Jatin Bodarya
  • 1,425
  • 2
  • 20
  • 32
1
2