Search code examples
rubydynamicsystemcron-task

Create an 'at' job in a script


In Ruby, I need to have a script queue up some at jobs. Making system calls is easy enough, I can use backticks, system, or %x(). But the at command is necessarily multi-line, and requires the command input ctrl+d to terminate. How can I create an at job dynamically?

Example (Ruby):

`at #{time}\nrun_other_script.rb\n:q`

Effect:

at 2014-10-14 11:30
at> run_other_script.rb
at> <EOT>

But of course my example doesn't work. What does?


Solution

  • at accepts input from standard input.

    Using IO::popen with w mode, you can send input to subprocess:

    IO.popen("at #{time}", "w") { |f|
      f.puts "run_other_script.rb"
    }
    

    You can also use open3 module.