如何每小时运行一个脚本,其中包含一些需要密码的命令?



我正在云桌面上工作并尝试执行./parseScript.sh,它具有以下命令:

我想每小时调用一次这个脚本,我正在使用以下命令:

Step 1. Connect to your cloud desktop using ssh.
Step 2. Run these commands:
% screen
% while true; do ./parseScript.sh; sleep 3600; done
Step 3. Close the window with the running command.
Step 4. (same as Step 1) Connect to your cloud desktop using ssh.
Step 5. Run this command: screen -R. You will get back to the session you left in step 3.

现在的问题是,在我的脚本中,我有上面提到的 1 个命令,必须使用 sudo -u identmon 执行,因此每次都会要求输入密码,除了手动之外,是否有每小时运行一次此脚本?

最简单的方法是添加NOPASSWD选项,您可以在此处查看 https://askubuntu.com/questions/147241/execute-sudo-without-password

username ALL=(ALL) NOPASSWD: ALL

也就是说,请注意您正在添加无密码sudo。

您可以使用 bash 的预期功能自动执行脚本。将密码存储在某个变量中。期望实用程序将在运行时自动向脚本提供密码。

最新更新