[opensuse] Run script when a program ends
Hello: I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that? I could write a script that makes a copy of the file, but don't know how to start the script when the program quits. Any idea or solution is welcomed. Thanks in advance, Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Op woensdag 16 oktober 2019 16:09:07 CEST schreef Istvan Gabor:
Hello:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
I could write a script that makes a copy of the file, but don't know how to start the script when the program quits.
Any idea or solution is welcomed.
Thanks in advance,
Istvan Easiest: Launch the program from a bash script, f.e.
#!/bin/bash program cp src dest -- Gertjan Lettink a.k.a. Knurpht openSUSE Board Member openSUSE Forums Team -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Wed, 16 Oct 2019 16:14:22 +0200, Knurpht-openSUSE wrote:
Op woensdag 16 oktober 2019 16:09:07 CEST schreef Istvan Gabor:
Hello:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
I could write a script that makes a copy of the file, but don't know how to start the script when the program quits.
Any idea or solution is welcomed.
Thanks in advance,
Istvan
Easiest: Launch the program from a bash script, f.e.
#!/bin/bash program cp src dest
This works perfectly in my case. Thanks! Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Istvan Gabor wrote:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
I presume there is more to it than this: ./program; cp file file2 -- Per Jessen, Zürich (15.5°C) http://www.hostsuisse.com/ - dedicated server rental in Switzerland. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Wed, Oct 16, 2019 at 11:08 AM Per Jessen <per@computer.org> wrote:
Istvan Gabor wrote:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
I presume there is more to it than this:
./program; cp file file2
If you program failed to run, or had a funky exist status that might inadvertently clobber data, so this would be a "safer" option. prgram && cp file file1 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Wed, 16 Oct 2019 11:35:56 -0400, Darin Perusich wrote:
On Wed, Oct 16, 2019 at 11:08 AM Per Jessen <per@computer.org> wrote:
Istvan Gabor wrote:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
I presume there is more to it than this:
./program; cp file file2
If you program failed to run, or had a funky exist status that might inadvertently clobber data, so this would be a "safer" option.
prgram && cp file file1
Thanks for the note. I will change the script accordingly. Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
Istvan Gabor wrote:
Hello:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
The info you give is a little sparse. If the suggested program ; cp .... (I'd probably use program && cp .... ) is not an option, another way could be using inotifywait. But that will only work if the file is always the same, or at least written to the same directory. E.g., I run a script (permamnently in background) that watches when my observers change a config file, and then saves a timestamped copy to a separate directory. Maybe it gives some inspiration :) #!/bin/sh file=/home/obs/CHROMIS/linedef.py dn=$(dirname $file) fn=$(basename $file) archive=/root/CHROMIS-calibarchive inotifywait -q -m -e close_write --format %f $dn | \ while read line ; do if [ $line = $fn ]; then cp $file $archive/$fn-$(date +%Y.%m.%d-%H:%M:%S) ; fi ; done -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
We have a c program that, if it is appropriate, spawns a process that lets the original program exit. This new process does what ever is in the script. The original program can be restarted before the script completes. The script can do anything. Is this what you mean? Roger
On Oct 16, 2019, at 19:10, Peter Suetterlin <pit@astro.su.se> wrote:
Istvan Gabor wrote:
Hello:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
The info you give is a little sparse. If the suggested program ; cp .... (I'd probably use program && cp .... ) is not an option, another way could be using inotifywait. But that will only work if the file is always the same, or at least written to the same directory. E.g., I run a script (permamnently in background) that watches when my observers change a config file, and then saves a timestamped copy to a separate directory. Maybe it gives some inspiration :)
#!/bin/sh file=/home/obs/CHROMIS/linedef.py dn=$(dirname $file) fn=$(basename $file) archive=/root/CHROMIS-calibarchive inotifywait -q -m -e close_write --format %f $dn | \ while read line ; do if [ $line = $fn ]; then cp $file $archive/$fn-$(date +%Y.%m.%d-%H:%M:%S) ; fi ; done
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Thu, 17 Oct 2019 00:41:48 +0300, Roger Oberholtzer wrote:
We have a c program that, if it is appropriate, spawns a process that lets the original program exit. This new process does what ever is in the script. The original program can be restarted before the script completes. The script can do anything. Is this what you mean?
Roger
I don't know. What do you mean by "if it is appropriate"? If it means that when the original program exits then yes, it would be useful. Nevertheless "program && ; cp file to copy" works for me, it seems. Thank you for your answer. Istvan
On Oct 16, 2019, at 19:10, Peter Suetterlin <pit@astro.su.se> wrote:
Istvan Gabor wrote:
Hello:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
The info you give is a little sparse. If the suggested program ; cp .... (I'd probably use program && cp .... ) is not an option, another way could be using inotifywait. But that will only work if the file is always the same, or at least written to the same directory. E.g., I run a script (permamnently in background) that watches when my observers change a config file, and then saves a timestamped copy to a separate directory. Maybe it gives some inspiration :)
#!/bin/sh file=/home/obs/CHROMIS/linedef.py dn=$(dirname $file) fn=$(basename $file) archive=/root/CHROMIS-calibarchive inotifywait -q -m -e close_write --format %f $dn | \ while read line ; do if [ $line = $fn ]; then cp $file $archive/$fn-$(date +%Y.%m.%d-%H:%M:%S) ; fi ; done
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
17.10.2019 19:58, Istvan Gabor пишет:
"program && ; cp file to copy" works for me, it seems.
Either "&&" or ";" is redundant. This command line executes cp unconditionally. If this is what you want, "&&" is not needed. If you want to perform copy only after program completed successfully, ";" should be removed. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Thu, 17 Oct 2019 20:04:47 +0300, Andrei Borzenkov wrote:
17.10.2019 19:58, Istvan Gabor пишет:
"program && ; cp file to copy" works for me, it seems.
Either "&&" or ";" is redundant. This command line executes cp unconditionally. If this is what you want, "&&" is not needed. If you want to perform copy only after program completed successfully, ";" should be removed.
Thanks, Andrei. If I understand correctly I can have (replacing ; by newline): either: #! /bin/bash program cp original copy This unconditionally does the copy independently from program exit status. or: #! /bin/bash program && cp original copy This does copy only if the exit status of program is successful. Is this correct? Thanks, Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
17.10.2019 20:32, Istvan Gabor пишет:
On Thu, 17 Oct 2019 20:04:47 +0300, Andrei Borzenkov wrote:
17.10.2019 19:58, Istvan Gabor пишет:
"program && ; cp file to copy" works for me, it seems.
Either "&&" or ";" is redundant. This command line executes cp unconditionally. If this is what you want, "&&" is not needed. If you want to perform copy only after program completed successfully, ";" should be removed.
Thanks, Andrei.
If I understand correctly I can have (replacing ; by newline):
either:
#! /bin/bash program cp original copy
This unconditionally does the copy independently from program exit status.
or:
#! /bin/bash program && cp original copy
This does copy only if the exit status of program is successful.
Is this correct?
Yes. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
On Wed, 16 Oct 2019 17:10:26 +0100, Peter Suetterlin wrote:
Istvan Gabor wrote:
Hello:
I have a program which saves data in a file. It writes the file when the user quits the program. I would like to automatically save a copy of the data file when/after the program quits. How can I achieve that?
The info you give is a little sparse. If the suggested program ; cp .... (I'd probably use program && cp .... ) is not an option, another way could be using inotifywait. But that will only work if the file is always the same, or at least written to the same directory. E.g., I run a script (permamnently in background) that watches when my observers change a config file, and then saves a timestamped copy to a separate directory. Maybe it gives some inspiration :)
#!/bin/sh file=/home/obs/CHROMIS/linedef.py dn=$(dirname $file) fn=$(basename $file) archive=/root/CHROMIS-calibarchive inotifywait -q -m -e close_write --format %f $dn | \ while read line ; do if [ $line = $fn ]; then cp $file $archive/$fn-$(date +%Y.%m.%d-%H:%M:%S) ; fi ; done
I will look into this one too. Thanks Istvan -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org
participants (7)
-
Andrei Borzenkov
-
Darin Perusich
-
Istvan Gabor
-
Knurpht-openSUSE
-
Per Jessen
-
Peter Suetterlin
-
Roger Oberholtzer