简体   繁体   中英

How to give user input in cronjob script

I have a run a script in cronjob. But that script is taking some user input.

How can I handle such case?

If it's taking the input from stdin, make another script that will call your script with a pipe or a redirect.

#!/bin/sh
/foo/bar/my_command < my_input

You can also launch it as a shell command in your crontab:

0 * * * * /bin/sh -c "/foo/bar/my_command < my_input"
  1. Modify the script so it does not need user input.

  2. Feed the script its required input from a file, via < .

Just in case the "expected user input" is, by any chance, a password: Do not use option 2. Editing access authentification into a crontab job is various flavors of dangerous.

PS: That warning above is also true if you're thinking about expect , as suggested by mvp. It is basically the equivalent of writing down your password on a post-it and sticking it to your monitor: Easy, convenient, and not secure at all.

You can use expect to simulate manual user input into your script. This works even if your script does not accept redirection (possible if it needs to enter passwords).

In practice, I found that Perl Expect is much easier to write script for compared to bare expect .

如果您只需要在脚本中确认(是),crontab 可能如下所示:

0 * * * * /bin/sh -c "yes | /foo/bar/my_command"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM