snjksh
July 24, 2013, 4:02am
1
Frequently we need to stop or start services on Linux systems.
For exmaple : To start or stop vsftpd daemon
# /etc/init.d/vsftpd start
or
# /etc/init.d/vsftpd stop
Following script will check the vsftpd service and if it is running, it will be stopped or vice-versa.
# cat /bin/vsftpd
#!/bin/bash
/etc/init.d/vsftpd status|grep running
if
[ "$?" == "0" ]; then
/etc/init.d/vsftpd stop
else
/etc/init.d/vsftpd start
fi
# chmod 500 /bin/vsftpd
Now we can run just
# vsftpd
I hope this small script will save some microseconds of our busy schedule
I don't think I would call it something so confusing. Perhaps put it in /usr/local/bin to segregate it from anything supplied. I can't actually see the value of flipping it on/off when you would want to know the current state to decide what to do next anyway.
If you need to stop/start, then you would have to run your script twice hoping that it is already running. You could just issue:-
service vsftpd restart
This will always leave it running (unless there is a start-up error)
I hope that this helps.
Robin
Liverpool/Blackburn
UK
snjksh
July 24, 2013, 12:34pm
3
Thanks Robin.
It seems that I was not able to explain need of this script. Let me try again ...
Objective of the script is not to restart the vsftpd service. Rather to minimize the typing of long command.
Actually I have to stop or start ( not restart ) vsftpd daemon on some server frequently.
So I thought of writing this small script to save my time.
Does your init.d script not accept the restart command?
What's wrong with just making a choice between:-
service vsftpd stop
service vsftpd start
That way you will be certain what state you are going to. You could always put these as an alias thus:-
alias ftpoff="service vsftpd stop"
alias ftpon="service vsftpd start"
Would that not be neater?
Robin
1 Like
snjksh
July 29, 2013, 8:51am
6
Robin : your option is better than mine. I will certainly use that.