gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
使用shell循环编辑用户的crontab。
- User
使用shell循环编辑用户的crontab。
- Assistant
要使用shell循环编辑用户的crontab,您可以编写一个bash脚本,通过读取包含用户名的文件或列表,并为每个用户执行
crontab命令。下面是一个简单的示例。- 假设您有一个包含用户名的文本文件
usernames.txt,每行一个用户。例如:
user1 user2 user3- 创建一个名为
update_cron.sh的bash脚本,使用以下内容:
#!/bin/bash # 遍历用户名文件,按行读取 while read user; do # 输出当前处理的用户 echo "Updating crontab for user: $user" # 使用"crontab -l"获取当前用户的cron,并将其存储在变量中 current_cron=$(crontab -l -u "$user") # 将您要添加的新cron条目存储在一个变量中。例如,我们正在添加一个每小时运行的任务 new_cron="0 * * * * /path/to/script.sh" # 更新用户的crontab,将新的cron任务追加到其现有任务 (echo "$current_cron"; echo "$new_cron") | crontab -u "$user" - done < usernames.txt echo "Cron jobs updated for all users."- 更改脚本的权限,使其可执行:
chmod +x update_cron.sh- 以root用户身份运行
update_cron.sh脚本,以便能够编辑其他用户的crontab:
sudo ./update_cron.sh这将按顺序读取
usernames.txt文件中的每个用户,并在每个用户的crontab中添加新的cron任务。本示例仅向用户的crontab添加一个任务,但您可以根据需要添加多个任务,或根据需要对任务进行调整。 - 假设您有一个包含用户名的文本文件
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.