我有一台服务器,每次启动时,我都必须npm run dev
在终端上输入,以便启动 Node.js 服务器。如何使用 crontab 在每次启动时帮我完成此操作?
我想要这样的东西;
@reboot echo "npm run dev"
答案1
通过执行以下命令来编辑你的 crontab:
# crontab -e
第一次执行此操作时,它会询问您使用什么文本编辑器(例如 nano)。
然后将以下行添加到你的 crontab:
@reboot npm run dev
这将在每次启动时执行该命令。
答案2
要定义 cronjob,你可以打开终端并输入
crontab -e
使用
sudo crontab -e
以 root 用户身份执行此操作。这将打开您定义为文本编辑器的文本编辑器$EDITOR
。您可以在此处添加命令
@reboot npm run dev
在最后一行
文件看起来应该是这样的
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@reboot npm run dev
一旦保存并退出,它就会显示 crontab 已安装。