Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run crontab with user input

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?

like image 845
Herves Avatar asked Jul 19 '09 13:07

Herves


People also ask

Can I run cron job as non root user?

After the user is done with the crontab, the file is saved in /var/spool/cron/* for each user. Scheduled jobs will be run as the user, with the user's permissions not as root .

What is the use of * * * * * In cron?

* * * * * is a cron schedule expression wildcard, meaning your cron job should run every minute of every hour of every day of every month, each day of the week.

Does crontab run as user?

The root user crontabLike any other user, root has a user crontab. Essentially the same as any other user crontab, you are editing the root crontab when you run sudo crontab -e . Jobs scheduled in the root user crontab will be executed as root with all of its privileges.


2 Answers

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.

like image 200
Alex B Avatar answered Sep 21 '22 02:09

Alex B


  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
like image 22
Arkady Avatar answered Sep 21 '22 02:09

Arkady