0

I have a running bash script, that runs a program 'n' times in a loop. That program opens a txt for example, counts, then write another txt. At the start of the script, it deletes all of the output txts. But if I kill the running script with Ctrl-Z, and I want to delete all of the txt-s, or the program itself, I can't delete the txts that the program actually counted with, it writes the text is 'busy'. I could understand why it works like that, but in another ubuntu, i could delete all of them.

How can I handle this?

  • Can you trap the signal and then delete the files? See: https://stackoverflow.com/questions/20182454/shell-script-get-ctrlz-with-trap – Alan Kavanagh Aug 11 '17 at 10:29

1 Answers1

4

Ctrl-Z doesn't kill the script, it suspends it. The process remains alive, it can be resumed, and therefore it doesn't release the resources it is using.

If you want to kill the script, type Ctrl-C instead. When the script is really killed, there will be nothing to keep the file open and you will be able to delete.

janos
  • 120,954
  • 29
  • 226
  • 236
  • Super, thank you! When i read, how to kill a script, maybe it was Ctrl-C, but i could change it perhaps, by time... – Máté Brunner Aug 11 '17 at 10:36
  • Control-Z doesn't put the script in the background; it simply stops it from running. You would have to additionally use the `bg` command to continue running the script in the background. – chepner Aug 11 '17 at 11:44