M
M
Mikhail Shatilov2020-03-29 02:34:11
bash
Mikhail Shatilov, 2020-03-29 02:34:11

How to fix bash script to start all services when logging into WSL?

WSL v1 does not automatically start services at startup because there is no start itself. Instead, it is recommended to add the launch of the necessary services to the end of the ~/.bashrc file. This works, but is unsatisfactory: each login is delayed due to the start of all services, even when they are already running; duplicate lines are added to the file.

sudo service nginx start
sudo service php7.0-fpm start
sudo service php7.1-fpm start
sudo service php7.2-fpm start
sudo service php7.3-fpm start
sudo service mysql start
sudo service redis-server start

The idea is this: write a bash script that will be called from ~/.bashrc with the necessary parameters. I'm not good at writing scripts, so far it turned out like this:
#!/bin/bash

# run custom services
services_list=("nginx", "mysql")

for i in "${services_list[@]}"
do
  service_status=$(service $i status)
  if ; then
    sudo service $i --full-restart
  fi
done

Help fix it for a working version.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2020-03-29
@iproger

each login is delayed due to the start of all services, even when they are already running;

Add an & at the end of each and let it run in the background.
You can add >/dev/null &
duplicate lines are added to the file.

To what file?
Well, the bash script should look at least like this
#!/bin/bash

# run custom services
services_list="nginx mysql"

for svc in ${services_list}
do
  service_status=$(service $svc status)
  if ; then
    sudo service $svc --full-restart
  fi
done

But it is not very clear why you still need to start services at login, and not for another reason.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question