I'm writing a Bash script to make easier to start a local webserver that I use for testing purposes; I don't need it running all the time, it is more useful to me to start it when I need it.

Right now I need to run the following two commands to start it

sudo systemctl start apache2  
sudo systemctl start mysql

and these to stop it

sudo systemctl stop apache2  
sudo systemctl stop mysql

To make it easier, I wrote a simple Bash script, webserver-start, to start it:


#starts the webserver

sudo systemctl start apache2  
sudo systemctl start mysql

And a similar one to stop it.

I'd like to put everything in a single script file, and use some options to select if I want to start or stop (or even restart) the webserver, something in this fashion: webserver -s to start, webserver -p to stop, and so on.

How can I modify my scripts to make them accept different options and perform a different task based on them?


1 Answer 1


What you could do is the following:


systemctl $1 apache2 mysql

The benefit of this solution is that you can do the following:

  • sudo ./web.sh start to start everything.
  • sudo ./web.sh stop to stop everything.
  • sudo ./web.sh restart to restart.
  • And so on...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .