前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在Linux下设置脚本启动

如何在Linux下设置脚本启动

作者头像
胡齐
发布2019-11-12 18:08:23
2.5K0
发布2019-11-12 18:08:23
举报
文章被收录于专栏:运维猫运维猫

1、开机自动运行

假如Python自启动脚本为auto.py。那么用root权限编辑以下文件:

代码语言:javascript
复制
 [root@docker-01 ~]# vim /etc/rc.local 
 #!/bin/bash
 # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
 #
 # It is highly advisable to create own systemd services or udev rules
 # to run scripts during boot instead of using this file.
 #
 # In contrast to previous versions due to parallel execution during boot
 # this script will NOT be run after all other services.
 #
 # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
 # that this script will be executed during boot.
 
 touch/var/lock/subsys/local
 /usr/bin/python3 /home/selfcs/auto.py > /home/selfcs/auto.log

2、让脚本定时启动

用root权限编辑以下文件

代码语言:javascript
复制
 [root@docker-01 ~]# vim /etc/crontab
 ##创建定时任务
 [root@docker-01 ~]# crontab -e -u root ##-e修改 -u指定用户
 */10 * * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org && hwclock --systohc>/dev/null 2>&1

3、& 后台运行

将sh test.sh任务放到后台 ,关闭xshell,对应的任务也跟着停止。

代码语言:javascript
复制
 [root@docker-01 ~]# sh test.sh &
 [1] 17717

4、nohup不挂断的运行

用nohup运行命令可以使命令永久的执行下去,和用户终端没有关系,例如我们断开SSH连接都不会影响他的运行,注意了nohup没有后台运行的意思;&才是后台运行,通常nuhup和&在一起执行。

代码语言:javascript
复制
 [root@docker-01 ~]# nohup command > myout.file 2>&1 & ##本例子中,0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) ;
 2>&1是将标准错误(2)重定向到标准输出(&1),标准输出(&1)再被重定向输入到myout.file文件中。

5、创建一个配置文件

创建一个start.sh启动文件和stop.sh停止文件,把需要执行的命令放到脚本里面。

代码语言:javascript
复制
 [root@localhost bin]# cat start.sh 
 /usr/local/bin/mitmdump -p8888-s/home/smgadmin/web_crawler/scripts/dyscripty/bin/dyscripty.py >/dev/null 2>&1 &  ##启动文件
 [root@localhost bin]# sh start.sh 
 [root@localhost bin]# cat stop.sh 
 kill-9 `ps auxwww | grep 'mitmdump' | grep '/home/smgadmin/web_crawler/scripts/dyscripty/bin/dyscripty.py' | grep -v grep | awk '{print $2}'` ##停止文件
 [root@localhost bin]# sh stop.sh 
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维猫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、开机自动运行
  • 2、让脚本定时启动
  • 3、& 后台运行
  • 4、nohup不挂断的运行
  • 5、创建一个配置文件
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档