├── README.md ├── UNLICENSE ├── systemd ├── chinadns.service ├── letsencrypt.service ├── letsencrypt.timer ├── mongodb.service ├── mysqld.service ├── netdata.service ├── openresty.service ├── php-fpm.service └── rsync.service └── upstart ├── aria2c ├── awstats-cgi ├── goagent ├── hhvm ├── httpd ├── lixian ├── munin-server ├── mysqld ├── nginx ├── ocspd ├── php-fpm ├── redis-server ├── rsync-server ├── sslocal └── ssserver /README.md: -------------------------------------------------------------------------------- 1 | Linux Init Scripts 2 | ==================== 3 | 4 | ### Note: 5 | 6 | I am using Ubuntu on my servers and [systemd](http://www.freedesktop.org/wiki/Software/systemd/) has replaced [upstart](http://upstart.ubuntu.com) at Ubuntu 15.04 and later releases. So I will NOT update the script which I do not in use any more. 7 | 8 | -------------------- 9 | 10 | Some simple but useful init scripts in `/etc/init.d`. 11 | 12 | You need to add them to system services using `chkconfig`, `sysv-rc-conf`, `update-rc.d` or `insserv`. 13 | 14 | `sysv-rc-conf` will ignores `Required-Start`, `Required-Stop` and `Should-Start`, `Should-Stop`, and led startup sequences will be incorrect. Debian and Ubuntu users can use `update-rc.d` or `insserv` to set startup scripts. Please run `[sudo] ln -s /usr/lib/insserv/insserv /sbin/insserv` if can't find `insserv`. 15 | 16 | Please remove `/etc/init.d/.legacy-bootordering` for closing legacy mode, startup sequences may be incorrect too, if legacy mode enabled. 17 | 18 | Don't forget change the file permissions to 755 (`-rwxr-xr-x`), change the owner to root and change the group to root. 19 | 20 | Maybe you also need to modify the program path, the configuration file path, the log file path or the pid file path. 21 | 22 | In some Linux distributions (such as Debian, Ubuntu, Fedora, etc), they use tmpfs to mount `/run` temporarily. You must change the directory permissions to 777 (`drwxrwxrwx`) at system startup, to make the program doesn't run as root, has permissions to create a pid file in `/var/run`. 23 | 24 | For example, add `chmod 777 /run` to `/etc/init/mounted-run.conf`: 25 | 26 | ```shell 27 | # mounted-run - Populate and link to /run filesystem 28 | # 29 | # Populates the /run filesystem and adds compatibility links to it 30 | 31 | description "Populate and link to /run filesystem" 32 | 33 | start on mounted MOUNTPOINT=/run TYPE=tmpfs 34 | 35 | task 36 | 37 | script 38 | chmod 777 /run 39 | 40 | : > "/run/utmp" 41 | chmod 664 "/run/utmp" 42 | chgrp utmp "/run/utmp" 43 | 44 | # compatibility; should go away soon 45 | [ -d /dev/.initramfs/varrun ] && cp -a /dev/.initramfs/varrun/* /run/ || true 46 | 47 | mkdir -p /run/sendsigs.omit.d 48 | 49 | # Background the initial motd seeding 50 | #[ -d "/etc/update-motd.d" ] && run-parts --lsbsysinit /etc/update-motd.d > /run/motd & 51 | end script 52 | ``` 53 | 54 | ### WARNING 55 | 56 | Some init scripts may in conflict with the software own scripts. Such as rsync-server and rsync, munin-server and munin munin-node. **Do NOT use them together**! 57 | 58 | ### See also 59 | 60 | [The Guidelines for SysV-style Initscripts](https://en.opensuse.org/openSUSE:Packaging_init_scripts) 61 | 62 | 63 | Linux 启动脚本 64 | ============= 65 | 66 | ### 注意: 67 | 68 | 我在我的服务器上使用 Ubuntu,而在 Ubuntu 15.04 及之后版本中,[systemd](http://www.freedesktop.org/wiki/Software/systemd/) 替换了 [upstart](http://upstart.ubuntu.com),所以我不会再更新我不再使用的脚本。 69 | 70 | -------------------- 71 | 72 | 一些简单但有用的启动脚本,放在 `/etc/init.d` 中。 73 | 74 | 你需要使用 `chkconfig`、`sysv-rc-conf`、`update-rc.d` 或 `insserv` 添加它们到系统服务中。 75 | 76 | `sysv-rc-conf` 会忽略 `Required-Start`、`Required-Stop` 和 `Should-Start`、`Should-Stop`,从而导致启动顺序不正确,Debian 和 Ubuntu 用户可以使用 `update-rc.d` 或 `insserv` 设置启动项。如果找不到 `insserv`,请运行 `ln -s /usr/lib/insserv/insserv /sbin/insserv`。 77 | 78 | 请删除 `/etc/init.d/.legacy-bootordering` 以关闭 legacy mode,启用 legacy mode 也可能导致启动顺序不正确。 79 | 80 | 不要忘了更改文件权限为 755(`-rwxr-xr-x`)、所有者为 root、群组为 root。 81 | 82 | 你也可能需要更改程序路径、配置文件路径、日志文件路径或 pid 文件路径。 83 | 84 | 在一些 Linux 发行版中(如 Debian、Ubuntu 和 Fedora 等)使用 tmpfs 临时挂载 `/run`,必须在系统启动时把该目录的权限改为 777(`drwxrwxrwx`),才能使不以 root 身份运行的程序有权限在 `/var/run` 中创建 pid 文件。 85 | 86 | 比如在 `/etc/init/mounted-run.conf` 中加入 `chmod 777 /run`: 87 | 88 | ```shell 89 | # mounted-run - Populate and link to /run filesystem 90 | # 91 | # Populates the /run filesystem and adds compatibility links to it 92 | 93 | description "Populate and link to /run filesystem" 94 | 95 | start on mounted MOUNTPOINT=/run TYPE=tmpfs 96 | 97 | task 98 | 99 | script 100 | chmod 777 /run 101 | 102 | : > "/run/utmp" 103 | chmod 664 "/run/utmp" 104 | chgrp utmp "/run/utmp" 105 | 106 | # compatibility; should go away soon 107 | [ -d /dev/.initramfs/varrun ] && cp -a /dev/.initramfs/varrun/* /run/ || true 108 | 109 | mkdir -p /run/sendsigs.omit.d 110 | 111 | # Background the initial motd seeding 112 | #[ -d "/etc/update-motd.d" ] && run-parts --lsbsysinit /etc/update-motd.d > /run/motd & 113 | end script 114 | ``` 115 | 116 | ### 警告 117 | 118 | 部分启动脚本可能与软件自带脚本冲突,如 rsync-server 和 rsync,munin-server 和 munin、munin-node,**请勿同时使用**! 119 | 120 | ### 参见 121 | 122 | [制作 SysV 风格启动脚本的指南](https://zh.opensuse.org/openSUSE:Packaging_init_scripts) 123 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to [http://unlicense.org] 25 | -------------------------------------------------------------------------------- /systemd/chinadns.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Protect yourself against DNS poisoning in China. 3 | After=network.target auditd.service 4 | 5 | [Service] 6 | ExecStart=/usr/local/chinadns/bin/chinadns -c /usr/local/chinadns/share/chnroute.txt -b 127.0.0.1 -p 63 -s 223.5.5.5,223.6.6.6,114.114.114.114,114.114.115.115,8.8.8.8,8.8.4.4 -m 7 | Restart=on-failure 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | -------------------------------------------------------------------------------- /systemd/letsencrypt.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Renew Let’s Encrypt issued certificates 3 | After=syslog.target network.target 4 | 5 | [Service] 6 | Type=oneshot 7 | ExecStart=/usr/bin/certbot certonly -c /data/etc/letsencrypt/cli.ini 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | -------------------------------------------------------------------------------- /systemd/letsencrypt.timer: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Renew Let’s Encrypt issued certificates monthly 3 | 4 | [Timer] 5 | OnCalendar=monthly 6 | Persistent=true 7 | 8 | [Install] 9 | WantedBy=timers.target 10 | -------------------------------------------------------------------------------- /systemd/mongodb.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=High-performance, schema-free document-oriented database 3 | After=network.target 4 | 5 | [Service] 6 | User=mongodb 7 | PermissionsStartOnly=true 8 | ExecStartPre=/usr/bin/bash -c 'echo "never" > /sys/kernel/mm/transparent_hugepage/enabled; echo "never" > /sys/kernel/mm/transparent_hugepage/defrag' 9 | ExecStart=/usr/bin/mongod --quiet --config /data/etc/mongodb/mongodb.yaml 10 | User=mongodb 11 | Group=mongodb 12 | 13 | [Install] 14 | WantedBy=multi-user.target 15 | -------------------------------------------------------------------------------- /systemd/mysqld.service: -------------------------------------------------------------------------------- 1 | # 2 | # /etc/systemd/system/mariadb.service 3 | # 4 | # This file is free software; you can redistribute it and/or modify it 5 | # under the terms of the GNU Lesser General Public License as published by 6 | # the Free Software Foundation; either version 2.1 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # Thanks to: 10 | # Daniel Black 11 | # Erkan Yanar 12 | # David Strauss 13 | # and probably others 14 | 15 | [Unit] 16 | Description=MariaDB database server 17 | After=network.target 18 | After=syslog.target 19 | 20 | [Install] 21 | WantedBy=multi-user.target 22 | 23 | 24 | [Service] 25 | 26 | ############################################################################## 27 | ## Core requirements 28 | ## 29 | 30 | Type=notify 31 | 32 | # Setting this to true can break replication and the Type=notify settings 33 | # See also bind-address mysqld option. 34 | PrivateNetwork=false 35 | 36 | ############################################################################## 37 | ## Package maintainers 38 | ## 39 | 40 | User=mysql 41 | Group=mysql 42 | 43 | # To allow memlock to be used as non-root user if set in configuration 44 | CapabilityBoundingSet=CAP_IPC_LOCK 45 | 46 | # Prevent writes to /usr, /boot, and /etc 47 | ProtectSystem=full 48 | 49 | NoNewPrivileges=true 50 | 51 | PrivateDevices=true 52 | 53 | # Prevent accessing /home, /root and /run/user 54 | ProtectHome=true 55 | 56 | # Execute pre and post scripts as root, otherwise it does it as User= 57 | PermissionsStartOnly=true 58 | 59 | Environment=MYSQLD_OPTS='--defaults-file=/data/etc/mysql.cnf' 60 | 61 | # Perform automatic wsrep recovery. When server is started without wsrep, 62 | # galera_recovery simply returns an empty string. In any case, however, 63 | # the script is not expected to return with a non-zero status. 64 | # It is always safe to unset _WSREP_START_POSITION environment variable. 65 | ExecStartPre=/bin/sh -c "systemctl unset-environment _WSREP_START_POSITION" 66 | ExecStartPre=/bin/sh -c "VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ] && \ 67 | systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1" 68 | 69 | # Needed to create system tables etc. 70 | # ExecStartPre=/usr/bin/mysql_install_db -u mysql 71 | 72 | # Start main service 73 | # MYSQLD_OPTS here is for users to set in /etc/systemd/system/mariadb.service.d/MY_SPECIAL.conf 74 | # Use the [service] section and Environment="MYSQLD_OPTS=...". 75 | # This isn't a replacement for my.cnf. 76 | # _WSREP_NEW_CLUSTER is for the exclusive use of the script galera_new_cluster 77 | 78 | ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION 79 | 80 | 81 | # Unset _WSREP_START_POSITION environment variable. 82 | ExecStartPost=/bin/sh -c "systemctl unset-environment _WSREP_START_POSITION" 83 | 84 | KillMode=process 85 | KillSignal=SIGTERM 86 | 87 | # Don't want to see an automated SIGKILL ever 88 | SendSIGKILL=no 89 | 90 | # Restart crashed server only, on-failure would also restart, for example, when 91 | # my.cnf contains unknown option 92 | Restart=on-abort 93 | RestartSec=5s 94 | 95 | UMask=007 96 | 97 | ############################################################################## 98 | ## USERs can override 99 | ## 100 | ## 101 | ## by creating a file in /etc/systemd/system/mariadb.service.d/MY_SPECIAL.conf 102 | ## and adding/setting the following will override this file's settings. 103 | 104 | # Useful options not previously available in [mysqld_safe] 105 | 106 | # Kernels like killing mysqld when out of memory because its big. 107 | # Lets temper that preference a little. 108 | # OOMScoreAdjust=-600 109 | 110 | # Explicitly start with high IO priority 111 | # BlockIOWeight=1000 112 | 113 | # If you don't use the /tmp directory for SELECT ... OUTFILE and 114 | # LOAD DATA INFILE you can enable PrivateTmp=true for a little more security. 115 | # PrivateTmp=true 116 | 117 | ## 118 | ## Options previously available to be set via [mysqld_safe] 119 | ## that now needs to be set by systemd config files as mysqld_safe 120 | ## isn't executed. 121 | ## 122 | 123 | # Number of files limit. previously [mysqld_safe] open-file-limit 124 | LimitNOFILE=16364 125 | 126 | # Maximium core size. previously [mysqld_safe] core-file-size 127 | # LimitCore= 128 | 129 | # Nice priority. previously [mysqld_safe] nice 130 | # Nice=-5 131 | 132 | # Timezone. previously [mysqld_safe] timezone 133 | # Environment="TZ=UTC" 134 | 135 | # Library substitutions. previously [mysqld_safe] malloc-lib with explict paths 136 | # (in LD_LIBRARY_PATH) and library name (in LD_PRELOAD). 137 | # Environment="LD_LIBRARY_PATH=/path1 /path2" "LD_PRELOAD= 138 | 139 | # Flush caches. previously [mysqld_safe] flush-caches=1 140 | # ExecStartPre=sync 141 | # ExecStartPre=sysctl -q -w vm.drop_caches=3 142 | 143 | # numa-interleave=1 equalivant 144 | # Change ExecStart=numactl --interleave=all /usr/sbin/mysqld...... 145 | 146 | # crash-script equalivent 147 | # FailureAction= 148 | -------------------------------------------------------------------------------- /systemd/netdata.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Real time performance monitoring 3 | After=network.target httpd.service squid.service nfs-server.service mysqld.service named.service postfix.service 4 | 5 | [Service] 6 | Type=simple 7 | User=netdata 8 | Group=netdata 9 | ExecStart=/usr/bin/netdata -D -c /data/etc/netdata/netdata.conf 10 | 11 | # saving a big db on slow disks may need some time 12 | TimeoutStopSec=60 13 | 14 | [Install] 15 | WantedBy=multi-user.target 16 | -------------------------------------------------------------------------------- /systemd/openresty.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=A high performance web server and a reverse proxy server 3 | After=syslog.target network.target 4 | 5 | [Service] 6 | Type=forking 7 | PIDFile=/run/nginx.pid 8 | ExecStartPre=/opt/openresty/nginx/sbin/nginx -t -q -c /data/etc/nginx/nginx.conf 9 | ExecStartPost=/bin/bash -c 'i=1; while [ ! -s /run/test.pid ]; do [ $i -gt 20 ] && exit 1 || (( i++ )) && sleep 0.2; done' 10 | ExecStart=/opt/openresty/nginx/sbin/nginx -c /data/etc/nginx/nginx.conf 11 | ExecReload=/opt/openresty/nginx/sbin/nginx -s reload -c /data/etc/nginx/nginx.conf 12 | ExecStop=/opt/openresty/nginx/sbin/nginx -s quit -c /data/etc/nginx/nginx.conf 13 | 14 | [Install] 15 | WantedBy=multi-user.target 16 | -------------------------------------------------------------------------------- /systemd/php-fpm.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=The PHP FastCGI Process Manager 3 | After=syslog.target network.target 4 | 5 | [Service] 6 | Type=notify 7 | PIDFile=/run/php-fpm.pid 8 | ExecStart=/usr/bin/php-fpm --nodaemonize --fpm-config /data/etc/php/php-fpm.conf -c /data/etc/php/php.ini 9 | ExecReload=/bin/kill -USR2 $MAINPID 10 | 11 | [Install] 12 | WantedBy=multi-user.target 13 | -------------------------------------------------------------------------------- /systemd/rsync.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=rsync daemon 3 | After=syslog.target network.target 4 | 5 | [Service] 6 | Type=forking 7 | PIDFile=/run/rsyncd.pid 8 | ExecStart=/usr/bin/rsync --daemon --config=/data/etc/rsync/rsync.conf 9 | 10 | [Install] 11 | WantedBy=multi-user.target 12 | -------------------------------------------------------------------------------- /upstart/aria2c: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: aria2c 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: aria2 daemon 10 | # Description: aria2 is a lightweight multi-protocol & multi-source command-line download utility. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=aria2c 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which aria2c` to find the full path, or just use `$name` if the binary file is in `$PATH`. 28 | bin=/usr/bin/$name 29 | if [ ! -x "$bin" ]; then 30 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 31 | exit 5 32 | fi 33 | 34 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 35 | config=/etc/aria2.conf 36 | if [ ! -r "$config" ]; then 37 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 38 | exit 6 39 | fi 40 | 41 | ### In the most cases, the log file of program is in `/var/log`. You can find the path in the configuration file. 42 | log=/var/log/aria2.log 43 | 44 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 45 | pidFile=/var/run/$name.pid 46 | 47 | ### Modify these variables to your own path. 48 | dlDir=/media/Files/Downloads 49 | sessionFile=$dlDir/aria2.session 50 | 51 | ########## ########## 52 | ############################################################### 53 | 54 | script=$(basename "$0") 55 | 56 | getPid() { 57 | ### Delete spaces, tabs and newlines. 58 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 59 | 60 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 61 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 62 | rm -f $pidFile 63 | unset pid 64 | fi 65 | } 66 | 67 | 68 | doStart() { 69 | echo -en "\033[1;36m[I]\033[0m Starting $name." 70 | 71 | ### aria2 doesn't create these files automatically, so we need to create them manually. 72 | ### Modify 'pi' to your own username. 73 | if [ ! -d "$dlDir" ]; then 74 | mkdir -p "$dlDir" 75 | chown -R pi:pi "$dlDir" 76 | fi 77 | 78 | if [ ! -w "$sessionFile" ]; then 79 | touch "$sessionFile" 80 | fi 81 | 82 | if [ ! -w "$log" ]; then 83 | touch "$log" 84 | chown pi:pi "$log" 85 | fi 86 | 87 | ### I don't want it to run as root. aria2 doesn't create a pid file. 88 | if ! su pi -s /bin/bash -c "$bin --conf-path='$config'"; then 89 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 90 | exit 1 91 | fi 92 | 93 | pid=$(pgrep -f $bin) 94 | echo -n $pid > $pidFile 95 | 96 | ### Successfully start the program if the process exists after 5 seconds. 97 | for (( i=0; i<=5; i++ )); do 98 | if ! kill -0 $pid 2>/dev/null; then 99 | rm -f $pidFile 100 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 101 | exit 1 102 | fi 103 | 104 | sleep 1 105 | echo -n '.' 106 | done 107 | 108 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 109 | } 110 | 111 | doStop() { 112 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 113 | 114 | if ! kill $pid; then 115 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 116 | exit 1 117 | fi 118 | 119 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 120 | for (( i=0; i<=10; i++ )); do 121 | if ! kill -0 $pid 2>/dev/null; then 122 | rm -f $pidFile 123 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 124 | return 0 125 | fi 126 | 127 | sleep 1 128 | echo -n '.' 129 | done 130 | 131 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 132 | exit 1 133 | } 134 | 135 | case "$1" in 136 | start) 137 | getPid 138 | if [ $pid ]; then 139 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 140 | exit 0 141 | fi 142 | 143 | doStart 144 | ;; 145 | 146 | stop) 147 | getPid 148 | if [ -z $pid ]; then 149 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 150 | exit 0 151 | fi 152 | 153 | doStop 154 | ;; 155 | 156 | kill) 157 | getPid 158 | if [ -z $pid ]; then 159 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 160 | exit 0 161 | fi 162 | 163 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 164 | 165 | if ! kill -9 $pid; then 166 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 167 | exit 1 168 | fi 169 | 170 | rm -f $pidFile 171 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 172 | ;; 173 | 174 | restart) 175 | getPid 176 | if [ -z $pid ]; then 177 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 178 | doStart 179 | exit 0 180 | fi 181 | 182 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 183 | doStop 184 | doStart 185 | ;; 186 | 187 | force-reload) 188 | getPid 189 | if [ -z $pid ]; then 190 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 191 | exit 7 192 | fi 193 | 194 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 195 | doStop 196 | doStart 197 | ;; 198 | 199 | status) 200 | getPid 201 | if [ -z $pid ]; then 202 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 203 | exit 3 204 | fi 205 | 206 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 207 | ;; 208 | 209 | *) 210 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 211 | exit 3 212 | ;; 213 | esac 214 | 215 | exit 0 216 | -------------------------------------------------------------------------------- /upstart/awstats-cgi: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: awstats-cgi 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: awstats cgi 10 | # Description: AWStats is a free powerful and featureful tool that generates advanced web, streaming, ftp or mail server statistics, graphically. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=awstats-cgi 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which spawn-fcgi` and `which fcgiwrap` to find the full path, or just use `spawn-fcgi` and `fcgiwrap` if it is in `$PATH`. 28 | spawnFcgi=/usr/bin/spawn-fcgi 29 | fcgiwrap=/usr/sbin/fcgiwrap 30 | if [ ! -x "$spawnFcgi" ] || [ ! -x "$fcgiwrap" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find $spawnFcgi or $fcgiwrap, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 36 | pidFile=/var/run/awstats-cgi.pid 37 | 38 | socketFile=/tmp/awstats-cgi.sock 39 | forkChildrenNum=3 40 | 41 | ########## ########## 42 | ############################################################### 43 | 44 | script=$(basename "$0") 45 | 46 | getPid() { 47 | unset cgiPids 48 | 49 | i=0 50 | while read line; do 51 | line=$(echo -n "$line" | tr -d ' \t\n') 52 | 53 | if [ -z $line ] || ! kill -0 $line 2>/dev/null; then 54 | continue 55 | fi 56 | 57 | cgiPids[$i]=$line 58 | 59 | (( i++ )) 60 | done < <(awk 1 $pidFile 2>/dev/null) 61 | 62 | if [ ${#cgiPids[*]} -eq 0 ]; then 63 | rm -f $pidFile 64 | fi 65 | } 66 | 67 | doStart() { 68 | echo -en "\033[1;36m[I]\033[0m Starting $name." 69 | 70 | if ! $spawnFcgi -F $forkChildrenNum -s $socketFile -u awstats -g awstats -U awstats -G awstats -P $pidFile -- $fcgiwrap -f; then 71 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 72 | exit 1 73 | fi 74 | chmod 666 $socketFile 75 | 76 | getPid 77 | 78 | ### Successfully start the program if the process exists after 5 seconds. 79 | for (( i=0; i<=5; i++ )); do 80 | if [ ${#cgiPids[*]} -eq 0 ]; then 81 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 82 | exit 1 83 | fi 84 | 85 | for cgiPid in ${cgiPids[*]}; do 86 | if ! kill -0 $cgiPid 2>/dev/null; then 87 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 88 | exit 1 89 | fi 90 | done 91 | 92 | sleep 1 93 | echo -n '.' 94 | done 95 | 96 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 97 | } 98 | 99 | doStop() { 100 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 101 | 102 | for cgiPid in ${cgiPids[*]}; do 103 | if ! kill $cgiPid; then 104 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 105 | exit 1 106 | fi 107 | done 108 | 109 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 110 | for (( i=0; i<=11; i++ )); do 111 | sleep 1 112 | echo -n '.' 113 | 114 | for cgiPid in ${cgiPids[*]}; do 115 | if kill -0 $cgiPid 2>/dev/null; then 116 | if [ $i -ge 10 ]; then 117 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 118 | exit 1 119 | fi 120 | 121 | continue 122 | fi 123 | done 124 | 125 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 126 | break 127 | done 128 | } 129 | 130 | case "$1" in 131 | start) 132 | getPid 133 | if [ ${#cgiPids[*]} -gt 0 ]; then 134 | echo -e "\033[1;33m[W]\033[0m $name is already running, pids are ${cgiPids[*]}." 135 | exit 0 136 | fi 137 | 138 | doStart 139 | ;; 140 | 141 | stop) 142 | getPid 143 | if [ ${#cgiPids[*]} -eq 0 ]; then 144 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 145 | exit 0 146 | fi 147 | 148 | doStop 149 | ;; 150 | 151 | kill) 152 | getPid 153 | if [ ${#cgiPids[*]} -eq 0 ]; then 154 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 155 | exit 0 156 | fi 157 | 158 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 159 | 160 | for cgiPid in ${cgiPids[*]}; do 161 | if ! kill -9 $cgiPid 2>/dev/null; then 162 | echo -e "\033[1;31m[E]\033[0m Kill $name failed." 163 | exit 1 164 | fi 165 | done 166 | rm -f $pidFile 167 | 168 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 169 | ;; 170 | 171 | restart) 172 | getPid 173 | if [ ${#cgiPids[*]} -eq 0 ]; then 174 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 175 | doStart 176 | exit 0 177 | fi 178 | 179 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 180 | doStop 181 | doStart 182 | ;; 183 | 184 | force-reload) 185 | getPid 186 | if [ ${#cgiPids[*]} -eq 0 ]; then 187 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 188 | exit 7 189 | fi 190 | 191 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 192 | doStop 193 | doStart 194 | ;; 195 | 196 | status) 197 | getPid 198 | if [ ${#cgiPids[*]} -eq 0 ]; then 199 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 200 | exit 3 201 | fi 202 | 203 | echo -e "\033[1;32m[N]\033[0m $name is already running, pids are ${cgiPids[*]}." 204 | ;; 205 | 206 | *) 207 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 208 | exit 3 209 | ;; 210 | esac 211 | 212 | exit 0 213 | -------------------------------------------------------------------------------- /upstart/goagent: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: goagent 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: goagent client 10 | # Description: A gae proxy forked from gappproxy/wallproxy. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=goagent 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### goagent need python 2 to run it. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which python2.7` to find the full path, or just use `$name` if the binary file is in `$PATH`. 29 | python27=$(which python2.7) 30 | if [ ! -x "$python27" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find python, Try using 'which python2' or 'which python' to find it." >&2 32 | exit 5 33 | fi 34 | 35 | ### Edit this variable to your own path. 36 | app=/media/Software/Software/GoAgent/local/proxy.py 37 | if [ ! -r "$app" ]; then 38 | echo -e "\033[1;31m[E]\033[0m Can not find $app, please edit the path to your own and and try again." >&2 39 | exit 5 40 | fi 41 | 42 | ### In the most cases, the log file of program is in `/var/log`. You are free to modify this variable. 43 | log=/var/log/$name.log 44 | 45 | ### By default, the pid file of program is in `/var/run`. So you don't need to modify this variable. 46 | pidFile=/var/run/$name.pid 47 | 48 | ########## ########## 49 | ############################################################### 50 | 51 | script=$(basename "$0") 52 | 53 | getPid() { 54 | ### Delete spaces, tabs and newlines. 55 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 56 | 57 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 58 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 59 | rm -f $pidFile 60 | unset pid 61 | fi 62 | } 63 | 64 | doStart() { 65 | echo -en "\033[1;36m[I]\033[0m Starting $name." 66 | 67 | if ! ( cd $(dirname $app) && nohup $python27 $app >> "$log" 2>&1 & ); then 68 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 69 | exit 1 70 | fi 71 | 72 | pid=$! 73 | echo -n $pid > $pidFile 74 | 75 | ### Successfully start the program if the process exists after 5 seconds. 76 | for (( i=0; i<=5; i++ )); do 77 | if ! kill -0 $pid 2>/dev/null; then 78 | rm -f $pidFile 79 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 80 | exit 1 81 | fi 82 | 83 | sleep 1 84 | echo -n '.' 85 | done 86 | 87 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 88 | } 89 | 90 | doStop() { 91 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 92 | 93 | if ! kill $pid; then 94 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 95 | exit 1 96 | fi 97 | 98 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 99 | for (( i=0; i<=10; i++ )); do 100 | if ! kill -0 $pid 2>/dev/null; then 101 | rm -f $pidFile 102 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 103 | return 0 104 | fi 105 | 106 | sleep 1 107 | echo -n '.' 108 | done 109 | 110 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 111 | exit 1 112 | } 113 | 114 | case "$1" in 115 | start) 116 | getPid 117 | if [ $pid ]; then 118 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 119 | exit 0 120 | fi 121 | 122 | doStart 123 | ;; 124 | 125 | stop) 126 | getPid 127 | if [ -z $pid ]; then 128 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 129 | exit 0 130 | fi 131 | 132 | doStop 133 | ;; 134 | 135 | kill) 136 | getPid 137 | if [ -z $pid ]; then 138 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 139 | exit 0 140 | fi 141 | 142 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 143 | 144 | if ! kill -9 $pid; then 145 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 146 | exit 1 147 | fi 148 | 149 | rm -f $pidFile 150 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 151 | ;; 152 | 153 | restart) 154 | getPid 155 | if [ -z $pid ]; then 156 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 157 | doStart 158 | exit 0 159 | fi 160 | 161 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 162 | doStop 163 | doStart 164 | ;; 165 | 166 | force-reload) 167 | getPid 168 | if [ -z $pid ]; then 169 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 170 | exit 7 171 | fi 172 | 173 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 174 | doStop 175 | doStart 176 | ;; 177 | 178 | status) 179 | getPid 180 | if [ -z $pid ]; then 181 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 182 | exit 3 183 | fi 184 | 185 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 186 | ;; 187 | 188 | *) 189 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 190 | exit 3 191 | ;; 192 | esac 193 | 194 | exit 0 195 | -------------------------------------------------------------------------------- /upstart/hhvm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: hhvm 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog mysqld 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog mysqld 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: hhvm daemon 10 | # Description: HipHop Virtual Machine is an open-source virtual machine designed for executing programs written in Hack and PHP. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=hhvm 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which hhvm` to find the full path, or just use `$name` if the binary file is in `$PATH`. 28 | bin=/usr/local/hhvm/bin/$name 29 | if [ ! -x "$bin" ]; then 30 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 31 | exit 5 32 | fi 33 | 34 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 35 | config=/data/etc/php/php.ini 36 | if [ ! -r "$config" ]; then 37 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 38 | exit 6 39 | fi 40 | 41 | ### In the most cases, the log file of program is in `/var/log`. You are free to modify this variable. 42 | log=/data/log/php/$name.log 43 | 44 | ### By default, the pid file of program is in `/var/run`. So you don't need to modify this variable. 45 | pidFile=/var/run/$name.pid 46 | 47 | ### By default, the socket file of program is in `/tmp`. So you don't need to modify this variable. 48 | socketFile=/tmp/$name.sock 49 | 50 | ########## ########## 51 | ############################################################### 52 | 53 | script=$(basename "$0") 54 | 55 | getPid() { 56 | ### Delete spaces, tabs and newlines. 57 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 58 | 59 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 60 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 61 | ### hhvm won't remove the pid file and the socket file when it crashed. 62 | rm -f $pidFile $socketFile 63 | unset pid 64 | fi 65 | } 66 | 67 | doStart() { 68 | echo -en "\033[1;36m[I]\033[0m Starting $name." 69 | 70 | if [ ! -f "$log" ]; then 71 | touch "$log" 72 | chown php:php "$log" 73 | fi 74 | 75 | ### I don't want it to run as root. But the program may be doesn't have permissions to create the log file. 76 | ### So you need to create them manually, and change owner & group. You can find the path in the configuration file. 77 | if ! su php -s /bin/bash -c "$bin -c '$config' -m daemon"; then 78 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 79 | exit 1 80 | fi 81 | 82 | sleep 1 83 | echo -n '.' 84 | 85 | ### The default permission of the socket file is 600. 86 | chmod 666 $socketFile 87 | 88 | getPid 89 | 90 | ### Successfully start the program if the process exists after 5 seconds. 91 | for (( i=0; i<=5; i++ )); do 92 | if ! kill -0 $pid 2>/dev/null; then 93 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 94 | exit 1 95 | fi 96 | 97 | sleep 1 98 | echo -n '.' 99 | done 100 | 101 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 102 | } 103 | 104 | doStop() { 105 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 106 | 107 | if ! kill $pid; then 108 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 109 | exit 1 110 | fi 111 | 112 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 113 | for (( i=0; i<=10; i++ )); do 114 | if ! kill -0 $pid 2>/dev/null; then 115 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 116 | return 0 117 | fi 118 | 119 | sleep 1 120 | echo -n '.' 121 | done 122 | 123 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 124 | exit 1 125 | } 126 | 127 | case "$1" in 128 | start) 129 | getPid 130 | if [ $pid ]; then 131 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 132 | exit 0 133 | fi 134 | 135 | doStart 136 | ;; 137 | 138 | stop) 139 | getPid 140 | if [ -z $pid ]; then 141 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 142 | exit 0 143 | fi 144 | 145 | doStop 146 | ;; 147 | 148 | kill) 149 | getPid 150 | if [ -z $pid ]; then 151 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 152 | exit 0 153 | fi 154 | 155 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 156 | 157 | if ! kill -9 $pid; then 158 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 159 | exit 1 160 | fi 161 | 162 | rm -f $pidFile 163 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 164 | ;; 165 | 166 | restart) 167 | getPid 168 | if [ -z $pid ]; then 169 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 170 | doStart 171 | exit 0 172 | fi 173 | 174 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 175 | doStop 176 | doStart 177 | ;; 178 | 179 | force-reload) 180 | getPid 181 | if [ -z $pid ]; then 182 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 183 | exit 7 184 | fi 185 | 186 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 187 | doStop 188 | doStart 189 | ;; 190 | 191 | status) 192 | getPid 193 | if [ -z $pid ]; then 194 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 195 | exit 3 196 | fi 197 | 198 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 199 | ;; 200 | 201 | *) 202 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 203 | exit 3 204 | ;; 205 | esac 206 | 207 | exit 0 208 | -------------------------------------------------------------------------------- /upstart/httpd: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: httpd 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: apache http server 10 | # Description: The Apache HTTP Server is a web server application notable for playing a key role in the initial growth of the World Wide Web. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=httpd 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### The recommended method of invoking the httpd executable is to use the apachectl control script. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which apachectl` to find the full path, or just use `apachectl` if it is in `$PATH`. 29 | bin=/usr/local/apache/bin/apachectl 30 | if [ ! -x "$bin" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 36 | config=/data/etc/apache/$name.conf 37 | if [ ! -r "$config" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 43 | ### But pay attention to `$name`, it may be different. You can find the path in the configuration file. 44 | pidFile=/var/run/$name.pid 45 | 46 | ########## ########## 47 | ############################################################### 48 | 49 | script=$(basename "$0") 50 | 51 | getPid() { 52 | ### Delete spaces, tabs and newlines. 53 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 54 | 55 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 56 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 57 | rm -f $pidFile 58 | unset pid 59 | fi 60 | } 61 | 62 | ### Successfully start the application if the process exists after 5 seconds. 63 | doStart() { 64 | echo -en "\033[1;36m[I]\033[0m Starting $name." 65 | 66 | ### apachectl is a script for running httpd, so we can't use its pid. 67 | if ! $bin -f "$config" & then 68 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 69 | exit 1 70 | fi 71 | 72 | getPid 73 | 74 | for (( i=0; i<=5; i++ )); do 75 | if ! kill -0 $pid 2>/dev/null; then 76 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 77 | exit 1 78 | fi 79 | 80 | sleep 1 81 | echo -n '.' 82 | done 83 | 84 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 85 | } 86 | 87 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 88 | doStop() { 89 | echo -en "\033[1;36m[I]\033[0m Gracefully stopping $name." 90 | 91 | if ! kill -WINCH $pid; then 92 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 93 | exit 1 94 | fi 95 | 96 | for (( i=0; i<=10; i++ )); do 97 | if ! kill -0 $pid 2>/dev/null; then 98 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 99 | return 0 100 | fi 101 | 102 | sleep 1 103 | echo -n '.' 104 | done 105 | 106 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 107 | exit 1 108 | } 109 | 110 | case "$1" in 111 | start) 112 | getPid 113 | if [ $pid ]; then 114 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 115 | exit 0 116 | fi 117 | 118 | doStart 119 | ;; 120 | 121 | stop) 122 | getPid 123 | if [ -z $pid ]; then 124 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 125 | exit 0 126 | fi 127 | 128 | doStop 129 | ;; 130 | 131 | kill) 132 | getPid 133 | if [ -z $pid ]; then 134 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 135 | exit 0 136 | fi 137 | 138 | echo -e "\033[1;36m[I]\033[0m Fast Shutdown $name..." 139 | 140 | if ! kill -TERM $pid; then 141 | echo -e "\033[1;31m[E]\033[0m Shutdown $name error." 142 | exit 1 143 | fi 144 | 145 | echo -e "\033[1;32m[N]\033[0m Shutdown $name done." 146 | ;; 147 | 148 | restart) 149 | getPid 150 | if [ -z $pid ]; then 151 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 152 | doStart 153 | exit 0 154 | fi 155 | 156 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 157 | doStop 158 | doStart 159 | ;; 160 | 161 | reload) 162 | getPid 163 | if [ -z $pid ]; then 164 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 165 | exit 7 166 | fi 167 | 168 | echo -e "\033[1;36m[I]\033[0m Reloading $name configuration file..." 169 | 170 | if ! kill -USR1 $pid; then 171 | echo -e "\033[1;31m[E]\033[0m Reload $name configuration file error." 172 | exit 1 173 | fi 174 | 175 | echo -e "\033[1;32m[N]\033[0m Reload $name configuration file done." 176 | ;; 177 | 178 | force-reload) 179 | getPid 180 | if [ -z $pid ]; then 181 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 182 | exit 7 183 | fi 184 | 185 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 186 | doStop 187 | doStart 188 | ;; 189 | 190 | status) 191 | getPid 192 | if [ -z $pid ]; then 193 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 194 | exit 3 195 | fi 196 | 197 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 198 | ;; 199 | 200 | *) 201 | echo "Usage: [sudo] $script {start|stop|kill|restart|reload|force-reload|status}." >&2 202 | exit 3 203 | ;; 204 | esac 205 | 206 | exit 0 207 | -------------------------------------------------------------------------------- /upstart/lixian: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: lixian 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog mysqld 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog mysqld 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: sinosky lixian server daemon 10 | # Description: xunlei lixian api & a website project base on it & a plugin for flexget. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=lixian 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### lixian need python 2 to run it. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which python2.7` to find the full path, or just use `$name` if the binary file is in `$PATH`. 29 | python27=$(which python2.7) 30 | if [ ! -x "$python27" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find python, Try using 'which python2' or 'which python' to find it." >&2 32 | exit 5 33 | fi 34 | 35 | ### Edit this variable to your own path. 36 | app=/data/root/$name/main.py 37 | if [ ! -r "$app" ]; then 38 | echo -e "\033[1;31m[E]\033[0m Can not find $app, please edit the path to your own and and try again." >&2 39 | exit 5 40 | fi 41 | 42 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 43 | config=/data/etc/$name.conf 44 | if [ ! -r "$config" ]; then 45 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 46 | exit 6 47 | fi 48 | 49 | ### In the most cases, the log file of program is in `/var/log`. You are free to modify this variable. 50 | log=/data/log/$name.app.log 51 | 52 | ### By default, the pid file of program is in `/var/run`. So you don't need to modify this variable. 53 | pidFile=/var/run/$name.pid 54 | 55 | ########## ########## 56 | ############################################################### 57 | 58 | script=$(basename "$0") 59 | 60 | getPid() { 61 | ### Delete spaces, tabs and newlines. 62 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 63 | 64 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 65 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 66 | rm -f $pidFile 67 | unset pid 68 | fi 69 | } 70 | 71 | doStart() { 72 | echo -en "\033[1;36m[I]\033[0m Starting $name." 73 | 74 | if [ ! -f "$log" ]; then 75 | touch "$log" 76 | chown lixian:lixian "$log" 77 | fi 78 | 79 | ### I don't want it to run as root. But the program may be doesn't have permissions to create the log file or the database file. 80 | ### So you need to create them manually, and change owner & group. You can find the paths in the configuration file. 81 | if ! su lixian -s /bin/bash -c "nohup $python27 $app --f='$config' >> '$log' 2>&1 &"; then 82 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 83 | exit 1 84 | fi 85 | 86 | pid=$(pgrep -f $app) 87 | echo -n $pid > $pidFile 88 | 89 | ### Successfully start the program if the process exists after 5 seconds. 90 | for (( i=0; i<=5; i++ )); do 91 | if ! kill -0 $pid 2>/dev/null; then 92 | rm -f $pidFile 93 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 94 | exit 1 95 | fi 96 | 97 | sleep 1 98 | echo -n '.' 99 | done 100 | 101 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 102 | } 103 | 104 | doStop() { 105 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 106 | 107 | if ! kill $pid; then 108 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 109 | exit 1 110 | fi 111 | 112 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 113 | for (( i=0; i<=10; i++ )); do 114 | if ! kill -0 $pid 2>/dev/null; then 115 | rm -f $pidFile 116 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 117 | return 0 118 | fi 119 | 120 | sleep 1 121 | echo -n '.' 122 | done 123 | 124 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 125 | exit 1 126 | } 127 | 128 | case "$1" in 129 | start) 130 | getPid 131 | if [ $pid ]; then 132 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 133 | exit 0 134 | fi 135 | 136 | doStart 137 | ;; 138 | 139 | stop) 140 | getPid 141 | if [ -z $pid ]; then 142 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 143 | exit 0 144 | fi 145 | 146 | doStop 147 | ;; 148 | 149 | kill) 150 | getPid 151 | if [ -z $pid ]; then 152 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 153 | exit 0 154 | fi 155 | 156 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 157 | 158 | if ! kill -9 $pid; then 159 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 160 | exit 1 161 | fi 162 | 163 | rm -f $pidFile 164 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 165 | ;; 166 | 167 | restart) 168 | getPid 169 | if [ -z $pid ]; then 170 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 171 | doStart 172 | exit 0 173 | fi 174 | 175 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 176 | doStop 177 | doStart 178 | ;; 179 | 180 | force-reload) 181 | getPid 182 | if [ -z $pid ]; then 183 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 184 | exit 7 185 | fi 186 | 187 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 188 | doStop 189 | doStart 190 | ;; 191 | 192 | status) 193 | getPid 194 | if [ -z $pid ]; then 195 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 196 | exit 3 197 | fi 198 | 199 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 200 | ;; 201 | 202 | *) 203 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 204 | exit 3 205 | ;; 206 | esac 207 | 208 | exit 0 209 | -------------------------------------------------------------------------------- /upstart/munin-server: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: munin 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: munin daemon 10 | # Description: Munin is a free and open-source computer system monitoring, network monitoring and infrastructure monitoring software application. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=munin 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which munin-node` `which spawn-fcgi` and `which fcgiwrap` to find the full path, or just use `munin-node` `spawn-fcgi` and `fcgiwrap` if it is in `$PATH`. 28 | node=/usr/local/munin/sbin/munin-node 29 | spawnFcgi=/usr/bin/spawn-fcgi 30 | fcgiwrap=/usr/sbin/fcgiwrap 31 | if [ ! -x "$node" ] || [ ! -x "$spawnFcgi" ] || [ ! -x "$fcgiwrap" ]; then 32 | echo -e "\033[1;31m[E]\033[0m Can not find $node $spawnFcgi or $fcgiwrap, maybe the program isn't installed." >&2 33 | exit 5 34 | fi 35 | 36 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 37 | config=/data/etc/munin/munin-node.conf 38 | if [ ! -r "$config" ]; then 39 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 40 | exit 6 41 | fi 42 | 43 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 44 | pidDir=/var/run/munin 45 | nodePidFile=$pidDir/munin-node.pid 46 | cgiPidFile=$pidDir/munin-cgi.pid 47 | 48 | cgiSocketFile=/tmp/munin-cgi.sock 49 | cgiForkChildrenNum=3 50 | 51 | ########## ########## 52 | ############################################################### 53 | 54 | script=$(basename "$0") 55 | 56 | getPid() { 57 | ### Delete spaces, tabs and newlines. 58 | nodePid=$(cat $nodePidFile 2>/dev/null | tr -d ' \t\n') 59 | 60 | ### `$nodePidFile` may be blank. If the process exists, `! kill -0 $nodePid 2>/dev/null` will return 1. 61 | if [ -z $nodePid ] || ! kill -0 $nodePid 2>/dev/null; then 62 | rm -f $nodePidFile 63 | unset nodePid 64 | fi 65 | 66 | unset cgiPids 67 | 68 | i=0 69 | while read line; do 70 | line=$(echo -n "$line" | tr -d ' \t\n') 71 | 72 | if [ -z $line ] || ! kill -0 $line 2>/dev/null; then 73 | continue 74 | fi 75 | 76 | cgiPids[$i]=$line 77 | 78 | (( i++ )) 79 | done < <(awk 1 $cgiPidFile 2>/dev/null) 80 | 81 | if [ ${#cgiPids[*]} -eq 0 ]; then 82 | rm -f $cgiPidFile 83 | fi 84 | } 85 | 86 | doStart() { 87 | echo -en "\033[1;36m[I]\033[0m Starting $name." 88 | 89 | if [ ! -d "$pidDir" ]; then 90 | mkdir -p $pidDir 91 | chown -R munin:munin $pidDir 92 | fi 93 | 94 | if ! $node --config "$config"; then 95 | echo -e "\n\033[1;31m[E]\033[0m Start munin-node error." 96 | exit 1 97 | fi 98 | 99 | ### WARNING: You can't specify a config file for munin-cgi. You must install munin from source if you want to use a custom config file. 100 | if ! $spawnFcgi -F $cgiForkChildrenNum -s $cgiSocketFile -u munin -g munin -U munin -G munin -P $cgiPidFile -- $fcgiwrap -f; then 101 | echo -e "\n\033[1;31m[E]\033[0m Start munin-cgi error." 102 | exit 1 103 | fi 104 | chmod 666 $cgiSocketFile 105 | 106 | getPid 107 | 108 | ### Successfully start the program if the process exists after 5 seconds. 109 | for (( i=0; i<=5; i++ )); do 110 | if ! kill -0 $nodePid 2>/dev/null; then 111 | echo -e "\n\033[1;31m[E]\033[0m Start munin-node failed." 112 | exit 1 113 | fi 114 | 115 | if [ ${#cgiPids[*]} -eq 0 ]; then 116 | echo -e "\n\033[1;31m[E]\033[0m Start munin-cgi failed." 117 | exit 1 118 | fi 119 | 120 | for cgiPid in ${cgiPids[*]}; do 121 | if ! kill -0 $cgiPid 2>/dev/null; then 122 | echo -e "\n\033[1;31m[E]\033[0m Start munin-cgi failed." 123 | exit 1 124 | fi 125 | done 126 | 127 | sleep 1 128 | echo -n '.' 129 | done 130 | 131 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 132 | } 133 | 134 | doStop() { 135 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 136 | 137 | if ! kill $nodePid 2>/dev/null; then 138 | echo -e "\n\033[1;31m[E]\033[0m Stop munin-node error. Try using $script kill." 139 | exit 1 140 | fi 141 | 142 | for cgiPid in ${cgiPids[*]}; do 143 | if ! kill $cgiPid; then 144 | echo -e "\n\033[1;31m[E]\033[0m Stop munin-cgi error. Try using $script kill." 145 | exit 1 146 | fi 147 | done 148 | 149 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 150 | for (( i=0; i<=11; i++ )); do 151 | sleep 1 152 | echo -n '.' 153 | 154 | if kill -0 $nodePid 2>/dev/null; then 155 | if [ $i -ge 10 ]; then 156 | echo -e "\n\033[1;31m[E]\033[0m Stop munin-node failed. Try using $script kill." 157 | exit 1 158 | fi 159 | 160 | continue 161 | fi 162 | 163 | for cgiPid in ${cgiPids[*]}; do 164 | if kill -0 $cgiPid 2>/dev/null; then 165 | if [ $i -ge 10 ]; then 166 | echo -e "\n\033[1;31m[E]\033[0m Stop munin-cgi failed. Try using $script kill." 167 | exit 1 168 | fi 169 | 170 | continue 171 | fi 172 | done 173 | 174 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 175 | break 176 | done 177 | } 178 | 179 | case "$1" in 180 | start) 181 | getPid 182 | if [ $nodePid ] || [ ${#cgiPids[*]} -gt 0 ]; then 183 | echo -e "\033[1;33m[W]\033[0m $name is already running:" 184 | 185 | if [ $nodePid ]; then 186 | echo -e "\033[1;33m[W]\033[0m munin-node pid = $nodePid." 187 | fi 188 | 189 | if [ ${#cgiPids[*]} -gt 0 ]; then 190 | echo -e "\033[1;33m[W]\033[0m munin-cgi pids are ${cgiPids[*]}." 191 | fi 192 | 193 | exit 0 194 | fi 195 | 196 | doStart 197 | ;; 198 | 199 | stop) 200 | getPid 201 | if [ -z $nodePid ] && [ ${#cgiPids[*]} -eq 0 ]; then 202 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 203 | exit 0 204 | fi 205 | 206 | doStop 207 | ;; 208 | 209 | kill) 210 | getPid 211 | if [ -z $nodePid ] && [ ${#cgiPids[*]} -eq 0 ]; then 212 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 213 | exit 0 214 | fi 215 | 216 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 217 | 218 | if ! kill -9 $nodePid 2>/dev/null; then 219 | echo -e "\033[1;31m[E]\033[0m Kill munin-node failed." 220 | exit 1 221 | fi 222 | rm -f $nodePidFile 223 | 224 | for cgiPid in ${cgiPids[*]}; do 225 | if ! kill -9 $cgiPid 2>/dev/null; then 226 | echo -e "\033[1;31m[E]\033[0m Kill munin-cgi failed." 227 | exit 1 228 | fi 229 | done 230 | rm -f $cgiPidFile 231 | 232 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 233 | ;; 234 | 235 | restart) 236 | getPid 237 | if [ -z $nodePid ] && [ ${#cgiPids[*]} -eq 0 ]; then 238 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 239 | doStart 240 | exit 0 241 | fi 242 | 243 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 244 | doStop 245 | doStart 246 | ;; 247 | 248 | force-reload) 249 | getPid 250 | if [ -z $nodePid ] && [ ${#cgiPids[*]} -eq 0 ]; then 251 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 252 | exit 7 253 | fi 254 | 255 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 256 | doStop 257 | doStart 258 | ;; 259 | 260 | status) 261 | getPid 262 | if [ -z $nodePid ] && [ ${#cgiPids[*]} -eq 0 ]; then 263 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 264 | exit 3 265 | fi 266 | 267 | echo -e "\033[1;32m[N]\033[0m $name is already running:" 268 | 269 | if [ $nodePid ]; then 270 | echo -e "\033[1;32m[N]\033[0m munin-node pid = $nodePid." 271 | fi 272 | 273 | if [ ${#cgiPids[*]} -gt 0 ]; then 274 | echo -e "\033[1;32m[N]\033[0m munin-cgi pids are ${cgiPids[*]}." 275 | fi 276 | 277 | ;; 278 | 279 | *) 280 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 281 | exit 3 282 | ;; 283 | esac 284 | 285 | exit 0 286 | -------------------------------------------------------------------------------- /upstart/mysqld: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: mysqld 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: mysql daemon 10 | # Description: MySQL is a very fast and reliable SQL database engine. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=mysqld 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### mysqld_safe is the recommended way to start a mysqld server on Unix. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which mysqld_safe` to find the full path, or just use `mysqld_safe` if it is in `$PATH`. 29 | bin=/usr/local/mysql/bin/mysqld_safe 30 | if [ ! -x "$bin" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 36 | config=/data/etc/mysql.cnf 37 | if [ ! -r "$config" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 43 | ### But pay attention to `$name`, it may be different. You can find the path in the configuration file. 44 | pidFile=/var/run/$name.pid 45 | 46 | ########## ########## 47 | ############################################################### 48 | 49 | script=$(basename "$0") 50 | 51 | getPid() { 52 | ### Delete spaces, tabs and newlines. 53 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 54 | 55 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 56 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 57 | rm -f $pidFile 58 | unset pid 59 | fi 60 | } 61 | 62 | doStart() { 63 | echo -en "\033[1;36m[I]\033[0m Starting $name." 64 | 65 | ### mysqld_safe is a script for running mysqld, so we can't use its pid. 66 | if ! $bin --defaults-file="$config" & then 67 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 68 | exit 1 69 | fi 70 | 71 | scriptPid=$! 72 | 73 | unset pid 74 | until [ $pid ]; do 75 | if ! kill -0 $scriptPid 2>/dev/null; then 76 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 77 | exit 1 78 | fi 79 | 80 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 81 | 82 | sleep 1 83 | echo -n '.' 84 | done 85 | 86 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 87 | } 88 | 89 | doStop() { 90 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 91 | 92 | if ! kill $pid; then 93 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error." 94 | exit 1 95 | fi 96 | 97 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 98 | for (( i=0; i<=20; i++ )); do 99 | if ! kill -0 $pid 2>/dev/null; then 100 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 101 | return 0 102 | fi 103 | 104 | sleep 1 105 | echo -n '.' 106 | done 107 | 108 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed." 109 | exit 1 110 | } 111 | 112 | case "$1" in 113 | start) 114 | getPid 115 | if [ $pid ]; then 116 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 117 | exit 0 118 | fi 119 | 120 | doStart 121 | ;; 122 | 123 | stop) 124 | getPid 125 | if [ -z $pid ]; then 126 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 127 | exit 0 128 | fi 129 | 130 | doStop 131 | ;; 132 | 133 | restart) 134 | getPid 135 | if [ -z $pid ]; then 136 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 137 | doStart 138 | exit 0 139 | fi 140 | 141 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 142 | doStop 143 | doStart 144 | ;; 145 | 146 | force-reload) 147 | getPid 148 | if [ -z $pid ]; then 149 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 150 | exit 7 151 | fi 152 | 153 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 154 | doStop 155 | doStart 156 | ;; 157 | 158 | status) 159 | getPid 160 | if [ -z $pid ]; then 161 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 162 | exit 3 163 | fi 164 | 165 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 166 | ;; 167 | 168 | *) 169 | ### You can't reload the configuration file in here, but you can use `SET` statement to modify server system variables dynamically. 170 | ### See https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html and https://dev.mysql.com/doc/refman/5.6/en/set-statement.html. 171 | ### You also can't forcefully kill mysqld, it's not safe. And mysqld_safe will rerun mysqld. 172 | # echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|reload|status}" >&2 173 | echo "Usage: [sudo] $script {start|stop|restart|force-reload|status}." >&2 174 | exit 3 175 | ;; 176 | esac 177 | 178 | exit 0 179 | -------------------------------------------------------------------------------- /upstart/nginx: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: nginx 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: nginx daemon 10 | # Description: nginx is an HTTP and reverse proxy server, as well as a mail proxy server. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=nginx 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which nginx` to find the full path, or just use `$name` if it is in `$PATH`. 28 | bin=/usr/local/nginx/sbin/$name 29 | if [ ! -x "$bin" ]; then 30 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 31 | exit 5 32 | fi 33 | 34 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 35 | config=/data/etc/nginx/$name.conf 36 | if [ ! -r "$config" ]; then 37 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 38 | exit 6 39 | fi 40 | 41 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. You can find the path in the configuration file. 42 | pidFile=/var/run/$name.pid 43 | 44 | ########## ########## 45 | ############################################################### 46 | 47 | script=$(basename "$0") 48 | 49 | getPid() { 50 | ### Delete spaces, tabs and newlines. 51 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 52 | 53 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 54 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 55 | rm -f $pidFile 56 | unset pid 57 | fi 58 | } 59 | 60 | doStart() { 61 | echo -en "\033[1;36m[I]\033[0m Starting $name." 62 | 63 | if ! $bin -c "$config"; then 64 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 65 | exit 1 66 | fi 67 | 68 | getPid 69 | 70 | ### Successfully start the program if the process exists after 5 seconds. 71 | for (( i=0; i<=5; i++ )); do 72 | if ! kill -0 $pid 2>/dev/null; then 73 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 74 | exit 1 75 | fi 76 | 77 | sleep 1 78 | echo -n '.' 79 | done 80 | 81 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 82 | } 83 | 84 | doStop() { 85 | echo -en "\033[1;36m[I]\033[0m Gracefully stopping $name." 86 | 87 | if ! kill -QUIT $pid; then 88 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 89 | exit 1 90 | fi 91 | 92 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 93 | for (( i=0; i<=10; i++ )); do 94 | if ! kill -0 $pid 2>/dev/null; then 95 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 96 | return 0 97 | fi 98 | 99 | sleep 1 100 | echo -n '.' 101 | done 102 | 103 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 104 | exit 1 105 | } 106 | 107 | case "$1" in 108 | start) 109 | getPid 110 | if [ $pid ]; then 111 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 112 | exit 0 113 | fi 114 | 115 | doStart 116 | ;; 117 | 118 | stop) 119 | getPid 120 | if [ -z $pid ]; then 121 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 122 | exit 0 123 | fi 124 | 125 | doStop 126 | ;; 127 | 128 | kill) 129 | getPid 130 | if [ -z $pid ]; then 131 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 132 | exit 0 133 | fi 134 | 135 | echo -e "\033[1;36m[I]\033[0m Fast Shutdown $name..." 136 | 137 | if ! kill -TERM $pid; then 138 | echo -e "\033[1;31m[E]\033[0m Shutdown $name error." 139 | exit 1 140 | fi 141 | 142 | echo -e "\033[1;32m[N]\033[0m Shutdown $name done." 143 | ;; 144 | 145 | restart) 146 | getPid 147 | if [ -z $pid ]; then 148 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 149 | doStart 150 | exit 0 151 | fi 152 | 153 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 154 | doStop 155 | doStart 156 | ;; 157 | 158 | reload) 159 | getPid 160 | if [ -z $pid ]; then 161 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 162 | exit 7 163 | fi 164 | 165 | echo -e "\033[1;36m[I]\033[0m Reloading $name configuration file..." 166 | 167 | if ! kill -HUP $pid; then 168 | echo -e "\033[1;31m[E]\033[0m Reload $name configuration file error." 169 | exit 1 170 | fi 171 | 172 | echo -e "\033[1;32m[N]\033[0m Reload $name configuration file done." 173 | ;; 174 | 175 | force-reload) 176 | getPid 177 | if [ -z $pid ]; then 178 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 179 | exit 7 180 | fi 181 | 182 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 183 | doStop 184 | doStart 185 | ;; 186 | 187 | status) 188 | getPid 189 | if [ -z $pid ]; then 190 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 191 | exit 3 192 | fi 193 | 194 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 195 | ;; 196 | 197 | reopen-log) 198 | getPid 199 | if [ -z $pid ]; then 200 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 201 | exit 7 202 | fi 203 | 204 | echo -e "\033[1;36m[I]\033[0m Relopen $name log files file..." 205 | 206 | if ! kill -USR1 $pid; then 207 | echo -e "\033[1;31m[E]\033[0m Relopen $name log files error." 208 | exit 1 209 | fi 210 | 211 | echo -e "\033[1;32m[N]\033[0m Relopen $name log files done." 212 | ;; 213 | 214 | *) 215 | echo "Usage: [sudo] $script {start|stop|kill|restart|reload|force-reload|status|reopen-log}." >&2 216 | exit 3 217 | ;; 218 | esac 219 | 220 | exit 0 221 | -------------------------------------------------------------------------------- /upstart/ocspd: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: ocspd 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: ocsp server 10 | # Description: r509-ocsp-responder powered by thin. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=ocspd 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which thin` to find the full path, or just use `thin` if it is in `$PATH`. 28 | bin=/usr/local/bin/thin 29 | if [ ! -x "$bin" ]; then 30 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 31 | exit 5 32 | fi 33 | 34 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 35 | config=/data/etc/ocsp.yml 36 | if [ ! -r "$config" ]; then 37 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 38 | exit 6 39 | fi 40 | 41 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 42 | pidFolder=/var/run/thin 43 | 44 | ########## ########## 45 | ############################################################### 46 | 47 | script=$(basename "$0") 48 | 49 | getPid() { 50 | ### `\d` doesn't work here. 51 | pidFiles=($(find $pidFolder -regex "$pidFolder/$name\.[0-9]+\.pid" 2>/dev/null)) 52 | pidFilesNum=${#pidFiles[@]} 53 | 54 | unset pids 55 | for (( i=0; i<$pidFilesNum; i++ )); do 56 | ### `${pidFiles[i]}` may be blank. 57 | pids[i]=$(cat ${pidFiles[i]} 2>/dev/null | tr -d ' \t\n') 58 | ### If the process exists, `kill -0 ${pids[i]} 2>/dev/null` will return 0. 59 | if [ "${pids[i]}" ] && kill -0 ${pids[i]} 2>/dev/null; then 60 | output+=" pid$i = ${pids[i]}" 61 | else 62 | rm -f ${pidFiles[i]} 63 | unset pids[i] 64 | fi 65 | done 66 | pidsNum=${#pids[@]} 67 | } 68 | 69 | doStart() { 70 | echo -en "\033[1;36m[I]\033[0m Starting $name." 71 | 72 | if [ ! -d /tmp/thin ]; then 73 | mkdir -p /tmp/thin 74 | chown -R thin:thin /tmp/thin 75 | fi 76 | 77 | ### I don't want it to run as root. But the program may be doesn't have permissions to create the log files or the socket files. 78 | ### So you need to create them manually, and change owner & group. You can find the paths in the configuration file. 79 | if ! su thin -s /bin/bash -c "$bin start -C $config"; then 80 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 81 | exit 1 82 | fi 83 | 84 | getPid 85 | 86 | if [ $pidsNum -ne 0 ]; then 87 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 88 | exit 1 89 | fi 90 | 91 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 92 | } 93 | 94 | doStop() { 95 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 96 | 97 | if ! su thin -s /bin/bash -c "$bin stop -C $config"; then 98 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error." 99 | exit 1 100 | fi 101 | 102 | getPid 103 | 104 | if [ $pidsNum -gt 0 ]; then 105 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed." 106 | exit 1 107 | fi 108 | 109 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 110 | } 111 | 112 | case "$1" in 113 | start) 114 | getPid 115 | if [ $pidsNum -gt 0 ]; then 116 | echo -e "\033[1;33m[W]\033[0m $name is already running.$output." 117 | exit 0 118 | fi 119 | 120 | doStart 121 | ;; 122 | 123 | stop) 124 | getPid 125 | if [ $pidsNum -ne 0 ]; then 126 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 127 | exit 0 128 | fi 129 | 130 | doStop 131 | ;; 132 | 133 | restart) 134 | getPid 135 | if [ $pidsNum -ne 0 ]; then 136 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 137 | doStart 138 | exit 0 139 | fi 140 | 141 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 142 | doStop 143 | doStart 144 | ;; 145 | 146 | force-reload) 147 | getPid 148 | if [ $pidsNum -ne 0 ]; then 149 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 150 | exit 7 151 | fi 152 | 153 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 154 | doStop 155 | doStart 156 | ;; 157 | 158 | status) 159 | getPid 160 | if [ $pidsNum -ne 0 ]; then 161 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 162 | exit 3 163 | fi 164 | 165 | echo -e "\033[1;32m[N]\033[0m $name is running.$output." 166 | ;; 167 | 168 | *) 169 | echo "Usage: [sudo] $script {start|stop|restart|force-reload|status}." >&2 170 | exit 3 171 | ;; 172 | esac 173 | 174 | exit 0 175 | -------------------------------------------------------------------------------- /upstart/php-fpm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: php-fpm 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog mysqld 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog mysqld 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: php-fpm daemon 10 | # Description: PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=php-fpm 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which php-fpm` to find the full path, or just use `$name` if it is in `$PATH`. 28 | bin=/usr/local/php/sbin/$name 29 | if [ ! -x "$bin" ]; then 30 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 31 | exit 5 32 | fi 33 | 34 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 35 | config=/data/etc/php/$name.conf 36 | ini=/data/etc/php/php.ini 37 | if [ ! -r "$config" ] || [ ! -r "$ini" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config or $ini) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. You can find the path in the configuration file. 43 | pidFile=/var/run/$name.pid 44 | 45 | ########## ########## 46 | ############################################################### 47 | 48 | script=$(basename "$0") 49 | 50 | getPid() { 51 | ### Delete spaces, tabs and newlines. 52 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 53 | 54 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 55 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 56 | rm -f $pidFile 57 | unset pid 58 | fi 59 | } 60 | 61 | doStart() { 62 | echo -en "\033[1;36m[I]\033[0m Starting $name." 63 | 64 | if ! $bin -c "$ini" -y "$config"; then 65 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 66 | exit 1 67 | fi 68 | 69 | getPid 70 | 71 | ### Successfully start the program if the process exists after 5 seconds. 72 | for (( i=0; i<=5; i++ )); do 73 | if ! kill -0 $pid 2>/dev/null; then 74 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 75 | exit 1 76 | fi 77 | 78 | sleep 1 79 | echo -n '.' 80 | done 81 | 82 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 83 | } 84 | 85 | doStop() { 86 | echo -en "\033[1;36m[I]\033[0m Gracefully stopping $name." 87 | 88 | if ! kill -QUIT $pid; then 89 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 90 | exit 1 91 | fi 92 | 93 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 94 | for (( i=0; i<=10; i++ )); do 95 | if ! kill -0 $pid 2>/dev/null; then 96 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 97 | return 0 98 | fi 99 | 100 | sleep 1 101 | echo -n '.' 102 | done 103 | 104 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 105 | exit 1 106 | } 107 | 108 | case "$1" in 109 | start) 110 | getPid 111 | if [ $pid ]; then 112 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 113 | exit 0 114 | fi 115 | 116 | doStart 117 | ;; 118 | 119 | stop) 120 | getPid 121 | if [ -z $pid ]; then 122 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 123 | exit 0 124 | fi 125 | 126 | doStop 127 | ;; 128 | 129 | kill) 130 | getPid 131 | if [ -z $pid ]; then 132 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 133 | exit 0 134 | fi 135 | 136 | echo -e "\033[1;36m[I]\033[0m Fast shutdown $name..." 137 | 138 | if ! kill -TERM $pid; then 139 | echo -e "\033[1;31m[E]\033[0m Shutdown $name error." 140 | exit 1 141 | fi 142 | 143 | echo -e "\033[1;32m[N]\033[0m Shutdown $name done." 144 | ;; 145 | 146 | restart) 147 | getPid 148 | if [ -z $pid ]; then 149 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 150 | doStart 151 | exit 0 152 | fi 153 | 154 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 155 | doStop 156 | doStart 157 | ;; 158 | 159 | reload) 160 | getPid 161 | if [ -z $pid ]; then 162 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 163 | exit 7 164 | fi 165 | 166 | echo -e "\033[1;36m[I]\033[0m Reloading $name configuration file..." 167 | 168 | if ! kill -USR2 $pid; then 169 | echo -e "\033[1;31m[E]\033[0m Reload $name configuration file error." 170 | exit 1 171 | fi 172 | 173 | echo -e "\033[1;32m[N]\033[0m Reload $name configuration file done." 174 | ;; 175 | 176 | force-reload) 177 | getPid 178 | if [ -z $pid ]; then 179 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 180 | exit 7 181 | fi 182 | 183 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 184 | doStop 185 | doStart 186 | ;; 187 | 188 | status) 189 | getPid 190 | if [ -z $pid ]; then 191 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 192 | exit 3 193 | fi 194 | 195 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 196 | ;; 197 | 198 | *) 199 | echo "Usage: [sudo] $script {start|stop|kill|restart|reload|force-reload|status}." >&2 200 | exit 3 201 | ;; 202 | esac 203 | 204 | exit 0 205 | -------------------------------------------------------------------------------- /upstart/redis-server: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: redis 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: redis daemon 10 | # Description: Redis is an open-source, networked, in-memory, key-value data store with optional durability. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=redis 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 27 | ### Try using `which redis-server` and `which redis-cli` to find the full path, or just use `redis-server` and `redis-cli` if it is in `$PATH`. 28 | bin=/usr/bin/redis-server 29 | cliBin=/usr/bin/redis-cli 30 | if [ ! -x "$bin" ] || [ ! -x "$cliBin" ] ; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find $bin or $cliBin, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 36 | config=/data/etc/$name.conf 37 | if [ ! -r "$config" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 43 | ### But pay attention to `$name`, it may be different. You can find the path in the configuration file. 44 | pidFile=/var/run/$name.pid 45 | 46 | ########## ########## 47 | ############################################################### 48 | 49 | script=$(basename "$0") 50 | 51 | getPid() { 52 | ### Delete spaces, tabs and newlines. 53 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 54 | 55 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 56 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 57 | rm -f $pidFile 58 | unset pid 59 | fi 60 | } 61 | 62 | doStart() { 63 | echo -en "\033[1;36m[I]\033[0m Starting $name." 64 | 65 | ### I don't want it to run as root. But the program may be doesn't have permissions to create the log file or the database directory. 66 | ### So you need to create them manually, and change owner & group. You can find the paths in the configuration file. 67 | if ! su redis -s /bin/bash -c "$bin '$config'"; then 68 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 69 | exit 1 70 | fi 71 | 72 | sleep 1 73 | echo -n '.' 74 | getPid 75 | 76 | ### Successfully start the program if the process exists after 5 seconds. 77 | for (( i=0; i<=5; i++ )); do 78 | if ! kill -0 $pid 2>/dev/null; then 79 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 80 | exit 1 81 | fi 82 | 83 | sleep 1 84 | echo -n '.' 85 | done 86 | 87 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 88 | } 89 | 90 | doStop() { 91 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 92 | 93 | if ! $cliBin shutdown; then 94 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error." 95 | exit 1 96 | fi 97 | 98 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 99 | for (( i=0; i<=10; i++ )); do 100 | if ! kill -0 $pid 2>/dev/null; then 101 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 102 | return 0 103 | fi 104 | 105 | sleep 1 106 | echo -n '.' 107 | done 108 | 109 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed." 110 | exit 1 111 | } 112 | 113 | case "$1" in 114 | start) 115 | getPid 116 | if [ $pid ]; then 117 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 118 | exit 0 119 | fi 120 | 121 | doStart 122 | ;; 123 | 124 | stop) 125 | getPid 126 | if [ -z $pid ]; then 127 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 128 | exit 0 129 | fi 130 | 131 | doStop 132 | ;; 133 | 134 | restart) 135 | getPid 136 | if [ -z $pid ]; then 137 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 138 | doStart 139 | exit 0 140 | fi 141 | 142 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 143 | doStop 144 | doStart 145 | ;; 146 | 147 | force-reload) 148 | getPid 149 | if [ -z $pid ]; then 150 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 151 | exit 7 152 | fi 153 | 154 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 155 | doStop 156 | doStart 157 | ;; 158 | 159 | status) 160 | getPid 161 | if [ -z $pid ]; then 162 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 163 | exit 3 164 | fi 165 | 166 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 167 | ;; 168 | 169 | *) 170 | ### You can't reload the configuration file in here, 171 | ### but you can use the `CONFIG SET` command which is used in order to reconfigure the server at run time without the need to restart Redis. 172 | ### See http://redis.io/commands/config-set. 173 | ### You also can't forcefully kill redis-server, it's not safe. 174 | # echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|reload|status}" >&2 175 | echo "Usage: [sudo] $script {start|stop|restart|force-reload|status}." >&2 176 | exit 3 177 | ;; 178 | esac 179 | 180 | exit 0 181 | -------------------------------------------------------------------------------- /upstart/rsync-server: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: rsync-server 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: rsync daemon 10 | # Description: rsync is an open source utility that provides fast incremental file transfer. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=rsyncd 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### rsync is built-in in most of linux distribution. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which rsync` to find the full path, or just use `$name` if the binary file is in `$PATH`. 29 | bin=$(which rsync) 30 | if [ ! -x "$bin" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find rsync, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 36 | config=/data/etc/rsync/$name.conf 37 | if [ ! -r "$config" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### By default, the pid file of program is in `/var/run`. So you don't need to edit this variable. 43 | ### But pay attention to `$name`, it may be different. You can find the path in the configuration file. 44 | pidFile=/var/run/$name.pid 45 | 46 | ########## ########## 47 | ############################################################### 48 | 49 | script=$(basename "$0") 50 | 51 | getPid() { 52 | ### Delete spaces, tabs and newlines. 53 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 54 | 55 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 56 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 57 | rm -f $pidFile 58 | unset pid 59 | fi 60 | } 61 | 62 | doStart() { 63 | echo -en "\033[1;36m[I]\033[0m Starting $name." 64 | 65 | if ! $bin --daemon --config="$config"; then 66 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 67 | exit 1 68 | fi 69 | 70 | getPid 71 | 72 | ### Successfully start the program if the process exists after 5 seconds. 73 | for (( i=0; i<=5; i++ )); do 74 | if ! kill -0 $pid 2>/dev/null; then 75 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 76 | exit 1 77 | fi 78 | 79 | sleep 1 80 | echo -n '.' 81 | done 82 | 83 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 84 | } 85 | 86 | 87 | doStop() { 88 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 89 | 90 | if ! kill $pid; then 91 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 92 | exit 1 93 | fi 94 | 95 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 96 | for (( i=0; i<=10; i++ )); do 97 | if ! kill -0 $pid 2>/dev/null; then 98 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 99 | return 0 100 | fi 101 | 102 | sleep 1 103 | echo -n '.' 104 | done 105 | 106 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 107 | exit 1 108 | } 109 | 110 | case "$1" in 111 | start) 112 | getPid 113 | if [ $pid ]; then 114 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 115 | exit 0 116 | fi 117 | 118 | doStart 119 | ;; 120 | 121 | stop) 122 | getPid 123 | if [ -z $pid ]; then 124 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 125 | exit 0 126 | fi 127 | 128 | doStop 129 | ;; 130 | 131 | kill) 132 | getPid 133 | if [ -z $pid ]; then 134 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 135 | exit 0 136 | fi 137 | 138 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 139 | 140 | if ! kill -9 $pid; then 141 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 142 | exit 1 143 | fi 144 | 145 | rm -f $pidFile 146 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 147 | ;; 148 | 149 | restart) 150 | getPid 151 | if [ -z $pid ]; then 152 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 153 | doStart 154 | exit 0 155 | fi 156 | 157 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 158 | doStop 159 | doStart 160 | ;; 161 | 162 | force-reload) 163 | getPid 164 | if [ -z $pid ]; then 165 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 166 | exit 7 167 | fi 168 | 169 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 170 | doStop 171 | doStart 172 | ;; 173 | 174 | status) 175 | getPid 176 | if [ -z $pid ]; then 177 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 178 | exit 3 179 | fi 180 | 181 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 182 | ;; 183 | 184 | *) 185 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 186 | exit 3 187 | ;; 188 | esac 189 | 190 | exit 0 191 | -------------------------------------------------------------------------------- /upstart/sslocal: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: sslocal 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: shadowsocks client 10 | # Description: A tunnel proxy which help you get through firewalls. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=sslocal 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### shadowsocks need python 2 or node.js to run it. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which sslocal` to find the full path, or just use `$name` if the binary file is in `$PATH`. 29 | bin=/usr/local/bin/$name 30 | if [ ! -x "$bin" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 36 | config=/etc/shadowsocks.json 37 | if [ ! -r "$config" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### In the most cases, the log file of program is in `/var/log`. You are free to modify this variable. 43 | log=/var/log/$name.log 44 | 45 | ### By default, the pid file of program is in `/var/run`. So you don't need to modify this variable. 46 | pidFile=/var/run/$name.pid 47 | 48 | ########## ########## 49 | ############################################################### 50 | 51 | script=$(basename "$0") 52 | 53 | getPid() { 54 | ### Delete spaces, tabs and newlines. 55 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 56 | 57 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 58 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 59 | rm -f $pidFile 60 | unset pid 61 | fi 62 | } 63 | 64 | doStart() { 65 | echo -en "\033[1;36m[I]\033[0m Starting $name." 66 | 67 | if ! $bin -c "$config" -d start --pid-file $pidFile --log-file "$log"; then 68 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 69 | exit 1 70 | fi 71 | 72 | getPid 73 | 74 | ### Successfully start the program if the process exists after 5 seconds. 75 | for (( i=0; i<=5; i++ )); do 76 | if ! kill -0 $pid 2>/dev/null; then 77 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 78 | exit 1 79 | fi 80 | 81 | sleep 1 82 | echo -n '.' 83 | done 84 | 85 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 86 | } 87 | 88 | doStop() { 89 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 90 | 91 | if ! $bin -c "$config" -d stop --pid-file $pidFile; then 92 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 93 | exit 1 94 | fi 95 | 96 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 97 | for (( i=0; i<=10; i++ )); do 98 | if ! kill -0 $pid 2>/dev/null; then 99 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 100 | return 0 101 | fi 102 | 103 | sleep 1 104 | echo -n '.' 105 | done 106 | 107 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 108 | exit 1 109 | } 110 | 111 | case "$1" in 112 | start) 113 | getPid 114 | if [ $pid ]; then 115 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 116 | exit 0 117 | fi 118 | 119 | doStart 120 | ;; 121 | 122 | stop) 123 | getPid 124 | if [ -z $pid ]; then 125 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 126 | exit 0 127 | fi 128 | 129 | doStop 130 | ;; 131 | 132 | kill) 133 | getPid 134 | if [ -z $pid ]; then 135 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 136 | exit 0 137 | fi 138 | 139 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 140 | 141 | if ! kill -9 $pid; then 142 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 143 | exit 1 144 | fi 145 | 146 | rm -f $pidFile 147 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 148 | ;; 149 | 150 | restart) 151 | getPid 152 | if [ -z $pid ]; then 153 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 154 | doStart 155 | exit 0 156 | fi 157 | 158 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 159 | doStop 160 | doStart 161 | ;; 162 | 163 | force-reload) 164 | getPid 165 | if [ -z $pid ]; then 166 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 167 | exit 7 168 | fi 169 | 170 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 171 | doStop 172 | doStart 173 | ;; 174 | 175 | status) 176 | getPid 177 | if [ -z $pid ]; then 178 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 179 | exit 3 180 | fi 181 | 182 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 183 | ;; 184 | 185 | *) 186 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 187 | exit 3 188 | ;; 189 | esac 190 | 191 | exit 0 192 | -------------------------------------------------------------------------------- /upstart/ssserver: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: ssserver 5 | # Required-Start: $local_fs $network $named $remote_fs $syslog 6 | # Required-Stop: $local_fs $network $named $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: shadowsocks server 10 | # Description: A tunnel proxy which help you get through firewalls. 11 | ### END INIT INFO 12 | 13 | PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 14 | export PATH 15 | 16 | if [ $(id -u) -ne 0 ]; then 17 | echo -e "\033[1;31m[E]\033[0m You must have root permissions for run this script, please use sudo and try again." >&2 18 | exit 4 19 | fi 20 | 21 | name=ssserver 22 | 23 | ############################################################### 24 | ########## Maybe you need to modify these variables. ########## 25 | 26 | ### shadowsocks need python 2 or node.js to run it. 27 | ### In the most cases, the binary file of program is in `/usr/bin` or `/usr/local/bin`. 28 | ### Try using `which $name` to find the full path, or just use `$name` if the binary file is in `$PATH`. 29 | bin=/usr/local/bin/ssserver 30 | if [ ! -x "$bin" ]; then 31 | echo -e "\033[1;31m[E]\033[0m Can not find $bin, maybe the program isn't installed." >&2 32 | exit 5 33 | fi 34 | 35 | ### In the most cases, the configuration file of program is in `/etc`. You can create your own configuration file and edit this variable to the new path. 36 | config=/etc/shadowsocks.json 37 | if [ ! -r "$config" ]; then 38 | echo -e "\033[1;31m[E]\033[0m The configuration file isn't found, maybe the path ($config) is wrong." >&2 39 | exit 6 40 | fi 41 | 42 | ### In the most cases, the log file of program is in `/var/log`. You are free to modify this variable. 43 | log=/var/log/$name.log 44 | 45 | ### By default, the pid file of program is in `/var/run`. So you don't need to modify this variable. 46 | pidFile=/var/run/$name.pid 47 | 48 | ########## ########## 49 | ############################################################### 50 | 51 | script=$(basename "$0") 52 | 53 | getPid() { 54 | ### Delete spaces, tabs and newlines. 55 | pid=$(cat $pidFile 2>/dev/null | tr -d ' \t\n') 56 | 57 | ### `$pidFile` may be blank. If the process exists, `! kill -0 $pid 2>/dev/null` will return 1. 58 | if [ -z $pid ] || ! kill -0 $pid 2>/dev/null; then 59 | rm -f $pidFile 60 | unset pid 61 | fi 62 | } 63 | 64 | doStart() { 65 | echo -en "\033[1;36m[I]\033[0m Starting $name." 66 | 67 | if ! $bin -c "$config" -d start --pid-file $pidFile --log-file "$log"; then 68 | echo -e "\n\033[1;31m[E]\033[0m Start $name error." 69 | exit 1 70 | fi 71 | 72 | getPid 73 | 74 | ### Successfully start the program if the process exists after 5 seconds. 75 | for (( i=0; i<5; i++ )); do 76 | if ! kill -0 $pid 2>/dev/null; then 77 | echo -e "\n\033[1;31m[E]\033[0m Start $name failed." 78 | exit 1 79 | fi 80 | 81 | sleep 1 82 | echo -n '.' 83 | done 84 | 85 | echo -e "\n\033[1;32m[N]\033[0m Start $name success." 86 | } 87 | 88 | doStop() { 89 | echo -en "\033[1;36m[I]\033[0m Stopping $name." 90 | 91 | if ! $bin -c "$config" -d stop --pid-file $pidFile; then 92 | echo -e "\n\033[1;31m[E]\033[0m Stop $name error. Try using $script kill." 93 | exit 1 94 | fi 95 | 96 | ### Successfully stop the program if the process doesn't exists in 10 seconds. 97 | for (( i=0; i<10; i++ )); do 98 | if ! kill -0 $pid 2>/dev/null; then 99 | echo -e "\n\033[1;32m[N]\033[0m Stop $name success." 100 | return 0 101 | fi 102 | 103 | sleep 1 104 | echo -n '.' 105 | done 106 | 107 | echo -e "\n\033[1;31m[E]\033[0m Stop $name failed. Try using $script kill." 108 | exit 1 109 | } 110 | 111 | case "$1" in 112 | start) 113 | getPid 114 | if [ $pid ]; then 115 | echo -e "\033[1;33m[W]\033[0m $name is already running. pid = $pid." 116 | exit 0 117 | fi 118 | 119 | doStart 120 | ;; 121 | 122 | stop) 123 | getPid 124 | if [ -z $pid ]; then 125 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 126 | exit 0 127 | fi 128 | 129 | doStop 130 | ;; 131 | 132 | kill) 133 | getPid 134 | if [ -z $pid ]; then 135 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 136 | fi 137 | 138 | echo -e "\033[1;36m[I]\033[0m Forcefully killing the process of $name..." 139 | 140 | if ! kill -9 $pid; then 141 | echo -e "\033[1;31m[E]\033[0m Kill $name error." 142 | exit 1 143 | fi 144 | 145 | rm -f $pidFile 146 | echo -e "\033[1;32m[N]\033[0m Kill $name done." 147 | ;; 148 | 149 | restart) 150 | getPid 151 | if [ -z $pid ]; then 152 | echo -e "\033[1;33m[W]\033[0m $name isn't running." 153 | doStart 154 | exit 0 155 | fi 156 | 157 | echo -e "\033[1;36m[I]\033[0m Restarting $name:" 158 | doStop 159 | doStart 160 | ;; 161 | 162 | force-reload) 163 | getPid 164 | if [ -z $pid ]; then 165 | echo -e "\033[1;31m[E]\033[0m $name isn't running." 166 | exit 7 167 | fi 168 | 169 | echo -e "\033[1;36m[I]\033[0m Forcefully reloading (restarting) $name:" 170 | doStop 171 | doStart 172 | ;; 173 | 174 | status) 175 | getPid 176 | if [ -z $pid ]; then 177 | echo -e "\033[1;32m[N]\033[0m $name isn't running." 178 | exit 3 179 | fi 180 | 181 | echo -e "\033[1;32m[N]\033[0m $name is running. pid = $pid." 182 | ;; 183 | 184 | *) 185 | echo "Usage: [sudo] $script {start|stop|kill|restart|force-reload|status}." >&2 186 | exit 3 187 | ;; 188 | esac 189 | 190 | exit 0 191 | --------------------------------------------------------------------------------