linux添加命令文件以及开机启动文件

在linux中将命令添加到系统中,直接运行。

安装程序后无法直接运行,需要将文件目录中的程序启动文件链接到某文件中。

 

需要再次强调,这是一种文件的管理方式而已,你甚至可以把自己的 binary 放到 $HOME/bin 下。还有,OS X 用 homebrew 安装的软件,会放在 /usr/local/Cellar 下,然后在 /usr/local/bin 创建一个指向相关 bin 目录的符号链接;但是在 Ubuntu 下,会放到 /usr/bin 下。

  • /usr/bin下面的都是系统预装的可执行程序,会随着系统升级而改变
  • /usr/local/bin目录是给用户放置自己的可执行程序的地方,推荐放在这里,不会被系统升级而覆盖同名文件
  • 理解2:
  • /bin 放置系统的关键程序,比如 ls cat ,对于“关键”的定义,不同的发行版会有不同的理解;
  • /usr/bin 放置发行版管理的程序,比如 Ubuntu 自带 md5sum ,这个 binary 就会在这个目录下;
  • /usr/local/bin 放置用户自己的程序,比如你编译了一个 gcc,那么 gcc 这个可执行 binary 应该在这个目录下;
  • 除此之外,还有对应的三个目录 /sbin /usr/sbin /usr/local/sbin ,放置系统管理的程序,比如 deluser chroot service ;

如果两个目录下有相同的可执行程序,谁优先执行受到PATH环境变量的影响,比如我的服务器的PATH变量为
echo $PATH 
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dean/bin 
这里/usr/local/bin优先于/usr/bin

/usr/bin或者/usr/local/bin或者其他运行的路径也不仅可以ln -s链接还可以将文件直接cp过去

演示:

        [root@localhost /]# cp /opt/php-5.6.31/sapi/fpm/init.d.php-fpm /usr/local/bin/php-fpm
        cp: ‘/opt/php-5.6.31/sapi/fpm/init.d.php-fpm’ and ‘/usr/local/bin/php-fpm’ are the same file
        [root@localhost /]# chmod +x /usr/local/bin/php-fpm
        [root@localhost /]# php-fpm stop
        Gracefully shutting down php-fpm . done
        [root@localhost /]# php-fpm start
        Starting php-fpm  done

 

开机启动程序需要做哪些操作?

yum安装的程序:

老版本的系统:chkconfig vsftpd on

#该命令是讲请求转发到system命令中

Note: Forwarding request to 'systemctl enable sshd.service'.

新版本系统:systemctl enable vsftpd

源码安装的程序:

(1)编辑文件 /etc/rc.local

输入命令:vim /etc/rc.local 将出现类似如下的文本片段:

        #!/bin/sh
        #
        # This script will be executed *after* all the other init scripts.
        # You can put your own initialization stuff in here if you don't
        # want to do the full Sys V style init stuff.

        touch /var/lock/subsys/local
        /etc/init.d/mysqld start #mysql开机启动
        /etc/init.d/nginx start #nginx开机启动
        /etc/init.d/php-fpm start #php-fpm开机启动
        /etc/init.d/memcached start #memcache开机启动

  • 这些就是开机的时候会自动执行的脚本和命令了。
  • 这个时候就有人问了,我以前写的是/etc/rc.local这个文件也没写过你说的/etc/rc.d/rc.local文件啊。这是因为/etc/rc.local软链接到/etc/rc.d/rc.local,所以写到哪个文件里都是一样的
  • 写在这个文件里的开机脚本都是默认后台执行的,不需要再加&符号了

(2)自己写一个shell脚本

将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,系统启动后就会自动执行该目录下的所有shell脚本。

(3)通过chkconfig命令设置

将启动文件cp到 /etc/init.d/或者/etc/rc.d/init.d/(前者是后者的软连接)下

vim 启动文件,文件前面务必添加如下三行代码,否侧会提示chkconfig不支持

        #!/bin/sh 告诉系统使用的shell,所以的shell脚本都是这样
        #chkconfig: 35 20 80 分别代表运行级别,启动优先权,关闭优先权,此行代码必须
        #description: http server(自己随便发挥)//两行都注释掉!!!,此行代码必须

chkconfig --add 脚本文件名 操作后就已经添加了

chkconfig命令设置 可设置优先级别

/etc/rc.d/init.d目录

为什么要介绍/etc/rc.local/init.d目录是因为要使用chkconfig来管理自动启动的脚本,首先将启动文件cp到 /etc/init.d/或者/etc/rc.d/init.d/(前者是后者的软连接)下才可以

1、init.d 目录中存放的是一系列系统服务的管理(启动与停止)脚本。
2、用service命令可执行init.d目录中相应服务的脚本。

例:执行命令“service resin start”,可启动/etc/init.d/resin脚本

[root@VM_0_15_centos init.d]# ls
abrt-ccpp  atd               cloud-config      crond      iptables    lvm2-lvmetad  netconsole  ntpd     psacct     restorecond  single
abrtd      auditd            cloud-final       functions  irqbalance  lvm2-monitor  netfs       ntpdate  quota_nld  rsyslog      sshd
abrt-oops  blk-availability  cloud-init        halt       kdump       mdmonitor     network     postfix  rdisc      sandbox      udev-post
acpid      bootlocal         cloud-init-local  ip6tables  killall     messagebus    nfs-rdma    pptpd    rdma       saslauthd    YDService
[root@VM_0_15_centos init.d]# cp pptpd pptpd2
[root@VM_0_15_centos init.d]# service pptpd2 status
pptpd (pid  1324) is running...
[root@VM_0_15_centos init.d]# /etc/rc.d/init.d/pptpd2 status
pptpd (pid  1324) is running...
  • 此目录下的脚本会被提供给service或者systemctl使用
  • 一般存在以下命令start、stop、reload、restart、force-reload大多数的情况下,你会使用到start,stop,restart选项
  • 当然了要使用init.d目录下的脚本,你需要有root权限或sudo权限。每个脚本都将被作为一个命令运行,每个脚本也至少需要755权限。
  • /etc/init.d指向/etc/rc.d/init.d目录

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值