Ubuntu 设置开机自动执行脚本

1. 建立service文件

sudo vim /etc/systemd/system/redis-server.service
2. redis service文件
[Unit]
Description=Advanced key-value store
After=network.target
Documentation=http://redis.io/documentation, man:redis-server(1)

[Service]
Type=notify
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf --supervised systemd --daemonize no
PIDFile=/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=2755

UMask=007
PrivateTmp=yes
LimitNOFILE=65535
PrivateDevices=yes
ProtectHome=yes
ReadOnlyDirectories=/
ReadWritePaths=-/var/lib/redis
ReadWritePaths=-/var/log/redis
ReadWritePaths=-/var/run/redis

NoNewPrivileges=true
CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE
MemoryDenyWriteExecute=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictRealtime=true
RestrictNamespaces=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX

# redis-server can write to its own config file when in cluster mode so we
# permit writing there by default. If you are not using this feature, it is
# recommended that you replace the following lines with "ProtectSystem=full".
ProtectSystem=true
ReadWriteDirectories=-/etc/redis

[Install]
WantedBy=multi-user.target
Alias=redis.service
3. postgresql service文件
[Unit]
Description=PostgreSQL 14 database server
Documentation=man:postgres(1)
Documentation=http://www.postgresql.org/docs/14/static/
After=network.target
 
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/bin/pg_ctl start -D /usr/local/postgresql-11.4/data -l /usr/local/postgresql-11.4/log/pg_server.log 
ExecStop=/usr/bin/pg_ctl stop -D /usr/local/postgresql-11.4/data -l /usr/local/postgresql-11.4/log/pg_server.log
ExecReload=/usr/bin/pg_ctl reload -D /usr/local/postgresql-11.4/data -l /usr/local/postgresql-11.4/log/pg_server.log
PIDFile=/usr/local/postgresql-11.4/data/postmaster.pid
TimeoutStopSec=0
Restart=always
User=postgres
Group=postgres
RuntimeDirectory=postgresql
RuntimeDirectoryMode=2755
UMask=007
PrivateTmp=yes
LimitNOFILE=65535
PrivateDevices=yes
ProtectHome=yes
ReadOnlyDirectories=/
ReadWriteDirectories=-/usr/local/postgresql-11.4
ReadWriteDirectories=-/etc/postgresql

[Install]
WantedBy=multi-user.target
Alias=postgresql.service
注:EnvironmentFile 参数可以将命令中使用的变量统一管理,例如$KUBECONFIG 这类型的变量,然后定义的变量可以在整个service文件中使用
4. 启动服务并检查状态
$ systemctl daemon-reload
$ systemctl start redis
$ systemctl status redis
$ systemctl restart redis

5. 开机自启动 

$ systemctl enable redis

你可能感兴趣的:(Ubuntu20,ubuntu,linux,运维)