Making scripts run at boot time with Debian

本文介绍在Debian系统中如何通过编辑init.d脚本来添加新的服务,并在系统启动时自动运行该服务的方法。通过使用update-rc.d命令创建相应的符号链接,可以轻松实现服务的开机启动与关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Posted by Steve on Mon 11 Oct 2004 at 13:01

Debian uses a Sys-V like init system for executing commands when the system runlevel changes - for example at bootup and shutdown time.

If you wish to add a new service to start when the machine boots you should add the necessary script to the directory /etc/init.d/. Many of the scripts already present in that directory will give you an example of the kind of things that you can do.

Here's a very simple script which is divided into two parts, code which always runs, and code which runs when called with "start" or "stop".

#! /bin/sh
# /etc/init.d/blah
#

# Some things that run always
touch /var/lock/blah

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script blah "
    echo "Could do more here"
    ;;
  stop)
    echo "Stopping script blah"
    echo "Could do more here"
    ;;
  *)
    echo "Usage: /etc/init.d/blah {start|stop}"
    exit 1
    ;;
esac

exit 0

Once you've saved your file into the correct location make sure that it's executable by running "chmod 755 /etc/init.d/blah".

Then you need to add the appropriate symbolic links to cause the script to be executed when the system goes down, or comes up.

The simplest way of doing this is to use the Debian-specific command update-rc.d:

root@skx:~# update-rc.d blah defaults
 Adding system startup for /etc/init.d/blah ...
   /etc/rc0.d/K20blah -> ../init.d/blah
   /etc/rc1.d/K20blah -> ../init.d/blah
   /etc/rc6.d/K20blah -> ../init.d/blah
   /etc/rc2.d/S20blah -> ../init.d/blah
   /etc/rc3.d/S20blah -> ../init.d/blah
   /etc/rc4.d/S20blah -> ../init.d/blah
   /etc/rc5.d/S20blah -> ../init.d/blah

If you wish to remove the script from the startup sequence in the future run:

root@skx:/etc/rc2.d# update-rc.d -f  blah remove
update-rc.d: /etc/init.d/blah exists during rc.d purge (continuing)
 Removing any system startup links for /etc/init.d/blah ...
   /etc/rc0.d/K20blah
   /etc/rc1.d/K20blah
   /etc/rc2.d/S20blah
   /etc/rc3.d/S20blah
   /etc/rc4.d/S20blah
   /etc/rc5.d/S20blah
   /etc/rc6.d/K20blah

This will leave the script itself in place, just remove the links which cause it to be executed.

You can find more details of this command by running "man update-rc.d".

<think>好的,用户想了解如何指定和运行npm脚本。首先我需要回忆一下npm脚本的基本概念和相关命令。根据用户提供的引用,特别是引用[1]和引用[2],里面有提到使用npm run-script或者npm run来执行脚本,还有预发布脚本的处理。 首先,用户可能想知道在package.json中如何定义脚本。这部分需要解释scripts字段的结构,比如键是脚本名称,值是要执行的命令。比如"start": "node app.js"这样的例子。然后需要说明如何运行这些脚本,使用npm run <script-name>,比如npm run start。不过对于预定义的脚本如start、test等,可以直接用npm start,而不需要run。 接下来,用户可能关心如何传递参数给脚本。这可以通过在命令后面添加--来处理,比如npm run lint -- --fix。这在实际使用中很常见,需要明确说明。 然后,用户可能想知道预脚本和后脚本的执行,比如pretest和posttest。这部分在引用[2]里有提到,当运行npm run test时,会自动执行pretest和posttest。需要举例子说明这个过程,让用户更容易理解。 另外,用户可能对运行依赖包中的脚本感兴趣,这时候需要引用引用[2]中的内容,使用npm explore <package> -- npm run <script>。这部分可能不太常用,但也是有用的功能,应该简要说明。 还有,用户可能会问到环境变量的问题,比如如何在脚本中访问npm提供的变量如npm_package_name。这部分可以举例说明,增强实用性。 最后,用户可能想知道如何组合多个脚本,使用&&或者并行执行。需要说明不同操作系统的差异,比如在Windows上可能需要使用不同的符号。 在整理这些点时,要确保结构清晰,分步骤说明,并给出具体的示例。同时,需要检查引用内容是否准确,引用标记是否正确添加。比如引用[1]提到npm run-script的用法,引用[2]提到预发布脚本和依赖脚本的执行。 用户的问题还可能包括错误处理,比如脚本执行失败怎么办,或者如何调试脚本。但根据提供的引用,可能不需要深入这部分,除非有相关信息。根据现有引用,主要集中在如何指定和运行脚本的基本操作,以及一些高级用法如参数传递和预/后脚本。 总结下来,回答的结构应该是: 1. 定义npm脚本:在package.json的scripts字段中添加键值对。 2. 运行脚本:使用npm run <script-name>,预定义脚本可以省略run。 3. 传递参数:使用--来分隔参数。 4. 预和后脚本:自动执行的机制。 5. 运行依赖包脚本:使用npm explore。 6. 环境变量:如何在脚本中使用。 7. 组合命令:使用&&或并行执行。 需要确保每个部分都有示例,并正确引用相关的引用来源。比如在解释预和后脚本时,引用[2]提到相关的内容,所以在该部分末尾添加[^2]。 最后,生成相关问题,可能包括如何调试npm脚本,如何处理脚本中的错误,如何跨平台运行脚本,或者如何管理复杂的脚本工作流。根据用户的问题,可能还会涉及npm脚本在CI/CD中的应用,或者与工具链集成的方法。</think>### 如何指定和运行npm脚本 #### 1. **定义npm脚本** 在项目的`package.json`文件中,通过`scripts`字段定义自定义脚本。例如: ```json { "scripts": { "start": "node app.js", "test": "jest", "build": "webpack --mode production" } } ``` - **键**(如`start`)为脚本名称,**值**(如`node app.js`)为需要执行的命令[^1][^3]。 #### 2. **运行脚本** - **基础命令**:使用`npm run <script-name>`,例如: ```bash npm run test ``` - **预定义脚本**:部分脚本(如`start`、`test`)可直接省略`run`: ```bash npm start ``` #### 3. **传递参数** 在命令后添加`--`分隔符传递参数: ```json { "scripts": { "lint": "eslint ." } } ``` 运行并传递参数: ```bash npm run lint -- --fix ``` 实际执行命令为`eslint . --fix`。 #### 4. **预脚本与后脚本** 若定义`pretest`和`posttest`,运行`npm run test`时会自动按顺序执行: ```json { "scripts": { "pretest": "echo '准备测试...'", "test": "jest", "posttest": "echo '测试完成!'" } } ``` 输出结果: ``` 准备测试... (Jest测试输出) 测试完成! ``` 此机制适用于所有自定义脚本。 #### 5. **运行依赖包中的脚本** 通过`npm explore <package> -- npm run <script>`执行其他包的脚本: ```bash npm explore some-package -- npm run build ``` 这会进入`some-package`目录并执行其`build`脚本。 #### 6. **环境变量** npm脚本中可通过`process.env`访问变量,例如: ```json { "scripts": { "info": "echo 项目名称:$npm_package_name" } } ``` 运行`npm run info`会输出项目名称。 #### 7. **组合命令** 使用`&&`串联命令(适用于Unix系统): ```json { "scripts": { "deploy": "npm run build && npm run publish" } } ``` Windows系统需使用`&`或跨平台工具(如`npm-run-all`)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值