是否可以在 crontab 中以 root 身份运行 Bash 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1015485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to make a Bash file run as root in crontab?
提问by omg
I need that Bashfile to run periodically, and it must be as root.
我需要定期运行该Bash文件,并且它必须以 root 身份运行。
Is that possible?
那可能吗?
[root@file nutch-0.9]# locate crontab
/etc/crontab
/etc/sysconfig/crontab
/usr/bin/crontab
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man1p/crontab.1p.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim71/syntax/crontab.vim
[root@file nutch-0.9]#
回答by whatsisname
Yes, just add it to the root users' crontab; run the crontab -e
command.
是的,只需将其添加到 root 用户的crontab 中即可;运行crontab -e
命令。
The places cron stores its files can be a little bizzare, so use the crontab -e
command which will make sure it's in the right place, and I believe it checks the syntax.
cron 存储其文件的位置可能有点奇怪,因此请使用该crontab -e
命令来确保它位于正确的位置,我相信它会检查语法。
回答by FreeMemory
You can just do
你可以做
crontab -e
as root.
作为根。
回答by sth
Just specify root
as the user for the entry in /etc/crontab
:
只需指定root
为 中条目的用户/etc/crontab
:
0 0 * * * root somecommand
Alternatively you can also add the command to root's personal crontab by using crontab -e
as root.
或者,您也可以以 root 身份将命令添加到 root 的个人 crontab 中crontab -e
。
回答by sth
No matter what, you will need to have access to the root user account.
无论如何,您都需要访问 root 用户帐户。
- you can add it to the root crontab, as suggested
- you can use sudo, as suggested
- you can use the setuid bit. The issue with the setuid bit is that it needs to be a compiled program. If it is compiled, you can "chmod 4755" and set the owner of the file to root, and it will run as root. If it is not compiled, you can write a tiny wrapper in C (or any other compiled programming language) that simply calls your script, and setuid on the wrapper, and make sure the wrapper is owned by root.
- 您可以按照建议将其添加到根 crontab
- 您可以按照建议使用 sudo
- 您可以使用 setuid 位。setuid 位的问题在于它需要是一个编译程序。如果编译成功,您可以“chmod 4755”并将文件的所有者设置为root,它将以root身份运行。如果它没有被编译,你可以用 C(或任何其他编译的编程语言)编写一个小包装器,它简单地调用你的脚本,并在包装器上 setuid,并确保包装器归 root 所有。
My advice? Use root crontab. It's what it's there for.
我的建议?使用根 crontab。这就是它的用途。
Also, there is no user entry in crontab as suggested by sth...the syntax is:
此外,在 crontab 中没有 sth 建议的用户条目......语法是:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
If you want to enter something in crontab as root, just login to your root account, "crontab -e" and voila...root crontab.
如果您想以 root 身份在 crontab 中输入一些内容,只需登录到您的 root 帐户,“crontab -e”和瞧...root crontab。
回答by rmeador
I believe all of the entries in root's crontab run as root. You can just make it invoke a Bash script as the action and it should do what you want.
我相信 root 的 crontab 中的所有条目都以 root 身份运行。你可以让它调用一个 Bash 脚本作为动作,它应该做你想做的。
回答by nolim1t
One way of doing this (via sudo):
这样做的一种方法(通过sudo):
- You need to set up sudo prilvileges for the account to run without entering in the user credentials
- Add "sudo /path/to/command" (without the ") to run the command as root. You can also add parameters to the command.
- 您需要为帐户设置 sudo prilvileges 以在不输入用户凭据的情况下运行
- 添加“sudo /path/to/command”(不带“)以root身份运行命令。您还可以向命令添加参数。