3

I am a developer working with LAMP stack.

My pc run arch linux so it is systemd based.

it has a limited amount of ram 8Gb:

  • sometime I use it for work
  • sometime for play games

I want to easily be able start/stop the LAMP services with one command.
So I can keep them disabled to save some ram.

How can I accomplish the above without writing every time:

sudo systemctl start httpd.service
sudo systemctl start mariadb.service
sudo systemctl start redis.service

I'll post below my solution in case some newbie bash lover need it, if you have a better solution please add it and I'll upvote it.

Thanks

2 Answers 2

4

First disable the services so they don't start at the boot:

sudo systemctl disable httpd.service
sudo systemctl disable mariadb.service
sudo systemctl disable redis.service

Then Edit .bashrc in your home and add to the end

web() {
    #do things with parameters like $1 such as
    sudo systemctl "$1" httpd.service
    sudo systemctl "$1" mariadb.service
    sudo systemctl "$1" redis.service
}

Now you can start/stop/restart all the related service with a simple

web start

Change web() with whatever you please.

4
  • 4
    This is a function, not an alias. That is actually a good thing; you should probably avoid aliases altogether.
    – tripleee
    Mar 21, 2018 at 9:45
  • Thanks for the note, why aliases are bad ?
    – Francesco
    Mar 21, 2018 at 13:22
  • 1
    E.g. unix.stackexchange.com/a/233751/19240 shows a few problems with them.
    – tripleee
    Mar 21, 2018 at 13:24
  • 1
    @Francesco, Aliases can't take arguments while functions can. That's why we should use functions and avoid aliases altogether. Mar 21, 2018 at 13:35
0

you can define and set an alias for start and one for stop like below command:

alias start_services='systemctl start httpd.service mariadb.service redis.service'
alias stop_services='systemctl stop httpd.service mariadb.service redis.service'

You must log in to answer this question.

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