Run crontab with user input

心已入冬 提交于 2019-12-10 15:37:19

问题


i created a crontab which will run a bash script test.sh. This test.sh file requires some input from the user, and saves the user input into a variable. How do i ensure that the user input will be saved to a variable in test.sh, and when crontab runs the script i can get the output i want?

for e.g i have 2 files, file1.sh and file2.sh. i put file2.sh in file 1.sh. i then run file1.sh, get the user input, and save it somewhere. crontab will run file2.sh, and retrieve the value from the "saved somewhere variable". is there anyway for this?


回答1:


If the input is read by the script from stdin, just redirect input from a file (using a wrapper script).

#! /bin/sh
test.sh < data.in

If this does not work for you (i.e. you have your script calling some interactive shell program like telnet, you can use Expect to automate the interaction.




回答2:


  1. file1.sh gets user input and writes it to /etc/file2.dat
  2. file2.sh reads /etc/file2.dat and does whatever it needs



回答3:


This seems like a strange thing to do. Ask yourself these questions:

Do you really want a popup asking the user for an input value every time the cron runs?

What happens when there's no one at the keyboard?



来源:https://stackoverflow.com/questions/1149858/run-crontab-with-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!