问题
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:
file1.sh
gets user input and writes it to/etc/file2.dat
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