# Create an application that restarts itself after n-seconds once [closed]

In your favorite programming language, create an application that restarts itself once, after n seconds.

You need to find a way to restart after exiting the program. EG: run cmd to do it for you. Make sure it waits an amount of seconds specified by the user before restarting

For clarification: You can run a second program, or compile one at runtime to do the work for you.

Also, make sure the program works every time you execute it, not only once.

Example of algorithm:

If hasntrestartedalready
exit
wait(i)
restart
end if


Least amount of bytes wins.

• 1. What does "Use any external programs/libraries that don't come with your OS" mean in the context of OSes other than Windows and MacOS? I don't think even CPM distros were consistent in their accompanying utils. 2. What does"invisible" mean? – Peter Taylor Dec 4 '16 at 15:04
• 1. Don't download binaries on runtime and use them to do the work for you, and don't have anything predownloaded to do the work for you. You can use any programming language, but you can't do eg DownloadFile("bar.com/foo.exe") and then run it. 2. Invisible = no gui/window/form showing. – P. Ktinos Dec 4 '16 at 15:26
• Why are you restricting the allowed algorithm? – TuxCrafting Dec 4 '16 at 16:16
• I'm not restricting it. I'm trying to give an example of how I did it. – P. Ktinos Dec 4 '16 at 16:18
• “The algorithm of your program should be:” If I'm correctly understandingthe meaning of “should” you are forbidding to use an other algorithm – TuxCrafting Dec 4 '16 at 16:23

# Batch, 34 28 bytes

timeout/t %1
if %1 neq [] %0


Input time in seconds as the first argument. If no argument is present, as well as after restarting, it spews out error messages (follow both commands with 2>NUL to get rid of them). The new instance opens in the same window (OP said it's allowed).

• Wait, if %1 isn't 1, then why is passing it to your script again going to work? Isn't it just infinite recursion based off of %1? Example: script 482 will never not call %0 %1. – NoOneIsHere Dec 4 '16 at 16:28
• Wouldn't it be something like if %1 neq 0 wait %1 %0 0? – NoOneIsHere Dec 4 '16 at 17:15
• Wouldn't you do if %1 neq 1 %0 1? – Timtech Dec 4 '16 at 17:19
• @Timtech That's what I proposed, but the current answer nor your proposal wait for %1 seconds, as required. – NoOneIsHere Dec 4 '16 at 18:19
• @Sobsz: in my answer, I used the operating system's task scheduler to handle the restart. (I think figuring out a way to do that is the hardest part of the challenge.) – user62131 Dec 4 '16 at 21:39

at now+$[$1/60]min<<<bash\ $0  # zsh + at, 28 bytes at now+$[$1/60]min<<<zsh\$0


# bash + at or zsh + at, 23 bytes, arguably cheating

at now+$[$1/60]min<<<$0  The first version re-executes the program after the number of seconds given as a command-line argument (which must be a multiple of 60 because at has only minute granularity). at is a standard POSIX program for scheduling programs to be run in the future. The 29-byte and 28-byte programs each rerun the program with the shell it's designed for. The 23-byte program doesn't specify a shell, and thus may well accidentally rerun it with sh, which is likely to be unable to parse it; it's up to you whether you consider it reasonable for the restart to happen in the wrong language or not, so I presented both versions. The time to wait is taken as a command-line argument. This means that the argument won't be present in the rerun, and thus the rerun will exit (with an error message due to the malformed expression, but which will be hidden by at; on some systems, it might be sent to you by email, but mine doesn't have that set up) rather than keep rerunning indefinitely. bash considers the $[] syntax for arithmetic obsolete, but it still works and is shorter than the "official" syntax. zsh is fine with it, as far as I could tell from the documentation.

• With your use of terms such as likely and acciddentally I will assume the program might not function correctly (restart after n-seconds) based on chance? If that's the case, that is not allowed, as the program must work each time it is executed. You can keep the other two however. – P. Ktinos Dec 5 '16 at 20:19
• It's not based on chance, it's based on the configuration of the operating system it's run under. Additionally, the program will restart; it's just that it might restart under the wrong interpreter. (It's analogous to a Python program that restarts after n seconds, but ends up running under Ruby rather than Python; the restart itself functions, but you can't say much about the functioning of the program after the restart.) – user62131 Dec 6 '16 at 5:17