简体   繁体   中英

how to autostart redis-server in ubuntu 20 desktop

I'm currently running Redis-server in my local development environment Ubuntu 20.04. Every time I open my desktop computer I always use this Redis-server --daemonize yes to start the Redis server. Is there a way to make it autostart when the desktop computer turned on?

如果您的系统正在使用systemd ,则在启动时启动它的方法是:

sudo systemctl enable redis-server

Not sure about redis, I thought it already gets installed as a service. In general you can use systemd. Something like the following:

cat << EOF > /etc/systemd/system/redis.service
[Unit]
Description=Start Redis Application
After=syslog.target
After=network.target[Service]
User=root
Type=simple

[Service]
WorkingDirectory=/root/
ExecStart=Redis-server --daemonize yes
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=redis

[Install]
WantedBy=multi-user.target


EOF

I fixed it using official redis documentation https://redis.io/topics/quickstart by Installing Redis more properly.

Here are the steps:

The following instructions can be used to perform a proper installation using the init script shipped with Redis 2.4 in a Debian or Ubuntu-based distribution.

Let's assume you already copied redis-server and redis-cli executables under /usr/local/bin.

1. Create a directory in which to store your Redis config files and your data:

  • sudo mkdir /etc/redis
  • sudo mkdir /var/redis

2. Copy the init script that you'll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:

  • sudo cp utils/redis_init_script /etc/init.d/redis_6379

3. Edit the init script.

  • sudo vi /etc/init.d/redis_6379

Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.

4. Copy the template configuration file you'll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:

  • sudo cp redis.conf /etc/redis/6379.conf

5. Create a directory inside /var/redis that will work as data and working directory for this Redis instance:

  • sudo mkdir /var/redis/6379

6. Edit the configuration file, making sure to perform the following changes:

  • Set daemonize to yes (by default it is set to no).
  • Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).
  • Change the port accordingly. In our example it is not needed as the default port is already 6379.
  • Set your preferred loglevel.
  • Set the logfile to /var/log/redis_6379.log
  • Set the dir to /var/redis/6379 (very important step!)

7. Finally add the new Redis init script to all the default runlevels using the following command:

  • sudo update-rc.d redis_6379 defaults

8. You are done! Now you can try running your instance with:

  • sudo /etc/init.d/redis_6379 start

Make sure that everything is working as expected:

  • Try pinging your instance with redis-cli .
  • Do a test save with redis-cli save and check that the dump file is - correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).
  • Check that your Redis instance is correctly logging in the log file.
  • If it's a new machine where you can try it without problems make sure that after a reboot everything is still working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM