You are not logged in.
I am working on an awk script which repeatedly does some work and then sleeps. Most of the time it is sleeping and while it is sleeping I would like to be able to hit control-c to abort the program. Example awk scripts I have seen suggest this should be possible by testing for a non-zero exit code from the sleep call. However, it doesn't work and my program won't let me stop it. The following test program prints "0" whether I abort the sleep with control-c or wait for it to terminate normally:
#!/bin/awk -f
BEGIN {
print system("sleep 10")
}
My arch system is up to date. From the "info" man page for sleep it says an abnormal termination should yield a non-zero exit code. I've tried calling other programs instead of sleep and they return non-zero exit codes when I hit control-c on them. I've tried invoking as "/bin/sleep 10" and "env sleep 10" as well. Nothing works. Any idea what I'm doing wrong?
Last edited by scottfial (2010-07-09 08:05:35)
Time flies like a banana.
Offline
I don't know, why your awk script does not work as expected, but the following works for me:
#!/bin/awk -f
BEGIN {
print system("trap 'exit 1' 2; sleep 10")
}
Last edited by ber_t (2010-06-22 08:06:03)
Offline
Your suggestion works ber_t (you knew that). Thanks! But I'm the kind of guy who still needs to know why his perfectly reasonable code didn't work. Specifically, why won't this loop terminate when I hit control-c:
#!/bin/awk -f
do {
# do stuff...
} while (!system("sleep 10"))
Its driving me crazy. The same basic approach works fine in lua:
#!/usr/bin/lua
repeat
-- do stuff
until os.execute("sleep 10") ~= 0
What the flip?
Time flies like a banana.
Offline
Well, after trying both, lua and awk, I made the observation, that lua executes the sleep directly, whereas awk uses a shell to run the system command:
$ ./test.lua &
$ ps axo pid,ppid,command
PID PPID COMMAND
2083 2081 bash
2434 2083 /usr/bin/lua ./test.lua
2435 2434 sleep 10
$ ./test.awk &
$ ps axo pid,ppid,command
PID PPID COMMAND
2083 2081 bash
2451 2083 /bin/awk -f ./test.awk
2452 2451 sh -c trap 'exit 1' 2; sleep 10
2453 2452 sleep 10
By default, the shell returns a zero exit status when hitting Ctrl-C. Using "trap 'exit 1' 2" tells her to exit appropriately.
Offline
I think the shell you're seeing might be getting created because of the "trap;sleep" combo. When I run your ps command against my test program using just "sleep 10" I don't see an invocation of "sh". It looks just like the lua version.
Time flies like a banana.
Offline
I think the shell you're seeing might be getting created because of the "trap;sleep" combo. When I run your ps command against my test program using just "sleep 10" I don't see an invocation of "sh". It looks just like the lua version.
You're absolutely correct. So we're both clueless and have to wait for others to join this thread.
Offline
Not sure I've got a complete handle on this, but this might be a useful pointer:
997 ~ $env sleep 10 || echo true
^C
997 ~ $env sleep 10 || echo true
^Z
[2]+ Stopped env sleep 10
true
997 ~ $env sleep 10 || echo true
^\Quit
true
It seems that SIGINT doesn't cause sleep to "fail", whereas SIGQUIT and SIGTSTP do. So (and I haven't tried wrapping this yet) it might be worth trying to quit the script with SIGQUIT or SIGTSTP + kill.
(edit clarification)
Last edited by skanky (2010-06-22 10:14:36)
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Sleep must return something useful when you it ^C because the following lua program:
#!/usr/bin/lua
local exit_code
repeat
exit_code = os.execute("sleep 10")
print("exit_code: " .. exit_code)
until exit_code ~= 0
produces the following output if I let the sleep expire twice and then hit ^C:
exit_code: 0
exit_code: 0
^Cexit_code: 2
and then it terminates. Now here's an equivalent awk program:
#!/bin/awk -f
BEGIN {
do {
exit_code = system("sleep 10")
print "exit_code: " exit_code
} while (!exit_code)
It produces the following if I let the sleep expire twice and then hit ^C twice:
exit_code: 0
exit_code: 0
^Cexit_code: 0
^Cexit_code: 0
and it never terminates. I have to kill it. Just to make sure that awk is seeing exit codes, I compiled this C program
int main(int argc, char *argv[]) {
return 2;
}
and changed my awk program to call "a.out" rather than "sleep 10". The result is
exit_code: 2
and then it terminates.
I got the following on the command line:
$ ./a.out
$ echo $?
2
$ sleep 10
^C
$ echo $?
130
Excuse me? 130? Lua saw a 2. None of this makes sense. Its as if awk has singled out "sleep" to not work! Where am I going wrong?
Last edited by scottfial (2010-06-22 17:18:54)
Time flies like a banana.
Offline
Ber_t is correct, the shell does return an exit code of 0 for Ctrl+C.
Here's a dodgy workaround to get your awk script running as intended
#!/bin/awk -f
BEGIN {
do {
exit_code = system("sleep 10 && exit 27")
print "exit_code: " exit_code
} while (exit_code != 0)
}
Offline