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
    read i
end if

Least amount of bytes wins.

  • \$\begingroup\$ 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? \$\endgroup\$ Dec 4, 2016 at 15:04
  • \$\begingroup\$ 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. \$\endgroup\$
    – Offtkp
    Dec 4, 2016 at 15:26
  • \$\begingroup\$ Why are you restricting the allowed algorithm? \$\endgroup\$ Dec 4, 2016 at 16:16
  • \$\begingroup\$ I'm not restricting it. I'm trying to give an example of how I did it. \$\endgroup\$
    – Offtkp
    Dec 4, 2016 at 16:18
  • \$\begingroup\$ “The algorithm of your program should be:” If I'm correctly understandingthe meaning of “should” you are forbidding to use an other algorithm \$\endgroup\$ Dec 4, 2016 at 16:23

2 Answers 2


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).

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Dec 4, 2016 at 16:28
  • \$\begingroup\$ Wouldn't it be something like if %1 neq 0 wait %1 %0 0? \$\endgroup\$ Dec 4, 2016 at 17:15
  • \$\begingroup\$ Wouldn't you do if %1 neq 1 %0 1? \$\endgroup\$
    – Timtech
    Dec 4, 2016 at 17:19
  • \$\begingroup\$ @Timtech That's what I proposed, but the current answer nor your proposal wait for %1 seconds, as required. \$\endgroup\$ Dec 4, 2016 at 18:19
  • 1
    \$\begingroup\$ @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.) \$\endgroup\$
    – user62131
    Dec 4, 2016 at 21:39

bash + at, 29 bytes

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.

  • \$\begingroup\$ 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. \$\endgroup\$
    – Offtkp
    Dec 5, 2016 at 20:19
  • \$\begingroup\$ 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.) \$\endgroup\$
    – user62131
    Dec 6, 2016 at 5:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.