To IF or not to IF

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
theblueplll
Posts: 154
Joined: 2019-04-29 01:17

To IF or not to IF

#1 Post by theblueplll »

I'll ask this again and try to get to the point and be more specific this time.

Is there a better way to write this?

Just some background:
I have tried using

Code: Select all

systemctl
to start and restart it and it doesn't work right.

Besides the developer of the application in question that uses this service(which is not a default service it comes from the app) says to use

Code: Select all

service xxx start or stop
.

Anyway the script runs fine has no errors and does what I want but there is always room to improve things.


Code: Select all

if [ "$EUID" -ne 0 ]  #if user is not root
		
		then                   #then use sudo
					
			sudo service xxx stop; 
					
				sudo service xxx start;
							
				else      #if the user is root
				
				
						service xxx stop;         #just refresh the service
					
					
						service xxx start;
                             
                             fi

I could probably just use

Code: Select all

service xx restart
also and shorten the

Code: Select all

if
up and make the script faster I suppose.

I want to put a check in their to see the service is running or not(which I have working I just didn't add it to this script yet)and an echo here and there to let the user know what is going on but I have been lazy about finishing it.

Any advice is appreciated.
Thanks.
Last edited by theblueplll on 2019-07-21 17:22, edited 2 times in total.

User avatar
Head_on_a_Stick
Posts: 13450
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: One More Try

#2 Post by Head_on_a_Stick »

theblueplll wrote:Any advice is appreciated
Post a link to the software in question and also post the exact errors when trying to use systemctl to start the service.

EDIT: and change the title to something descriptive to make it more useful to the community.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support

reinob
Posts: 935
Joined: 2014-06-30 11:42

Re: One More Try

#3 Post by reinob »

There's no general answer to your question, because nobody can know what the "start" action does in your particular initscript.

Some services will create a "pid file" storing the process id of the daemon they've started. But in your case, we don't even know if a daemon is started with "service .. start". Thus there's no definition of "being started".

Systemd can be useful in that it keeps a state of the service (unit) file but for some reason you don't want to use systemd.

You could also just look at the initscript and see what it does. If no pid file is created, either by the script or by the (supposedly) daemon being started, then you can at least check if a specific process with a specific name is started. Then your check can be a mere "ps aux | grep <something-specific-about-your-daemon> | grep -v grep"

(since you're asking again you are apparently not happy with just systemctl restart <service>, or "service <service> restart", thererefore I won't propose that answer again). You're gonna have to specify your question more precisely (yet another try).

jibberjabber
Posts: 165
Joined: 2016-01-10 16:58

Re: One More Try

#4 Post by jibberjabber »

Why are people still answering this fool.

It's bviously a bot or a really bored troll.

theblueplll
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

#5 Post by theblueplll »

reinob wrote:There's no general answer to your question, because nobody can know what the "start" action does in your particular initscript.

It is blatantly obvious that start means start and stop means stop.

I never asked if

Code: Select all

service start or stop xxx
was the right way to start or stop it.

Don't want to use systemd because I don't want it to rely on systemd and it is not technically a service or daemon(it can be ran as a daemon) it just runs in the back ground and you use

Code: Select all

service  nameofapplication start stop or restart
to start it stop it or restart it and I see no reason to use systemctl.

I had something that was checking the PID but it wasn't what I wanted because it would act like it was running when it wasn't because it would find just the PID not if it was active or not.

I wanted to know about the code itself and if there was a more efficient way to write it that is all.
theblueplll wrote:
Is there a better way to write this?
I never asked if I am starting and stopping it properly.
Head_on_a_Stick wrote:
theblueplll wrote:post the exact errors when trying to use systemctl to start the service.
There is no error I just tried it. Not sure what I was thinking when I said that.

I don't know what to say.
Is an IF the way to go or should I use something else is what I am looking for.
Should it be some type of variable that gets read(I imagine that would need an if also) or a string(not sure if that makes sense).
Maybe I don't know enough to do this who knows Haha.

Not trying to be a **** but I can show it off when it is finished but not yet and it's not because I don't wanted people to see it.
I have a reason that I don't want to get into for not showing the entire thing so can that just be respected instead of interrogating me and refusing to discuss it like the last time I posted about it?

User avatar
Head_on_a_Stick
Posts: 13450
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: One More Try

#6 Post by Head_on_a_Stick »

theblueplll wrote:Not trying to be a **** but I can show it off when it is finished but not yet and it's not because I don't wanted people to see it.
I have a reason that I don't want to get into for not showing the entire thing so can that just be respected instead of interrogating me and refusing to discuss it like the last time I posted about it?
There are two reasons why you should share the big picture with us:
  1. This might be an XY problem.
  2. These boards are a community resource rather than your personal help desk.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support

Dai_trying
Posts: 960
Joined: 2016-01-07 12:25

Re: To IF or not to IF

#7 Post by Dai_trying »

You could keep it short, like

Code: Select all

[ "$EUID" -ne 0 ] && sudo service xxx restart || service xxx restart
although I am not well versed enough to say if this is any better or worse than your code.

theblueplll
Posts: 154
Joined: 2019-04-29 01:17

Re: To IF or not to IF

#8 Post by theblueplll »

Dai_trying wrote:You could keep it short, like

Code: Select all

[ "$EUID" -ne 0 ] && sudo service xxx restart || service xxx restart
although I am not well versed enough to say if this is any better or worse than your code.
It never dawned on me to use OR.

I really need to stop where I am and learn the syntax better I suppose.

reinob
Posts: 935
Joined: 2014-06-30 11:42

Re: One More Try

#9 Post by reinob »

theblueplll wrote:
reinob wrote:There's no general answer to your question, because nobody can know what the "start" action does in your particular initscript.
It is blatantly obvious that start means start and stop means stop.
Something tells me this will be my last reply in this thread. You have not shown your initscript, you haven't told us what it does (i.e. how it runs the program you need to monitor), nor what that program does (like it if writes a pid file or is somehow identifiable).

Yet you think we are all stupid enough not to see the blatantly obvious.

So, again, you want us to tell you how to check if something has started. You have to tell us first:

** how do you (manually) check IF the program has started, i.e. if it is running or not **

Note that the above is entirely independent of HOW you start it (initscript, systemctl or manually).

If you (for some unknown reason) insist on using "service [...] restart" or even "systemctl restart [..]" then you have to show us the initscript, as for all I care that script is not doing anything, or running multiple programs, or rebooting a remote server.

User avatar
Bloom
Posts: 363
Joined: 2017-11-11 12:23

Re: To IF or not to IF

#10 Post by Bloom »

Dai_trying wrote:You could keep it short, like

Code: Select all

[ "$EUID" -ne 0 ] && sudo service xxx restart || service xxx restart
although I am not well versed enough to say if this is any better or worse than your code.
I would do this:

Code: Select all

[ "$EUID" -ne 0 ] && sudo echo "5 min cache of password"
sudo service xxx restart
If you type a password for sudo, the system caches it for the next 5 minutes. During that time, every sudo command you issue doesn't need the password to be given again.
Sudo will be ignored if you're already the root.
So this code will work when you're the root and ask for the sudo password if you're not the root.
For the record,

Code: Select all

systemctl restart xxx
should work just as well, since service calls systemctl nowadays.

theblueplll
Posts: 154
Joined: 2019-04-29 01:17

Re: To IF or not to IF

#11 Post by theblueplll »

Bloom wrote:
Dai_trying wrote:You could keep it short, like

Code: Select all

[ "$EUID" -ne 0 ] && sudo service xxx restart || service xxx restart
although I am not well versed enough to say if this is any better or worse than your code.
I would do this:

Code: Select all

[ "$EUID" -ne 0 ] && sudo echo "5 min cache of password"
sudo service xxx restart
If you type a password for sudo, the system caches it for the next 5 minutes. During that time, every sudo command you issue doesn't need the password to be given again.
Sudo will be ignored if you're already the root.
So this code will work when you're the root and ask for the sudo password if you're not the root.
For the record,

Code: Select all

systemctl restart xxx
should work just as well, since service calls systemctl nowadays.
Also something I wasn't thinking about even though I knew it wa the case.
So there is no reason for the added else since sudo would be ignored if the user is root.

Was actually thinking of using

Code: Select all

[ "$EUID" -ne 0 ]
to check for root and giving a warning that running things as such unless absolutely neccesary wasn't a good idea and exiting the script HAha.
There is really no reason to run my script as root since sudo will take care of things when it is needed.
'
Anyway that is the same root check I have used in the past but never tested it on anything but a Debian based OS.
I imagine it would work since it is a shell command not a specific OS command but who knows.

I actually was thinking about putting in a check for

User avatar
sickpig
Posts: 589
Joined: 2019-01-23 10:34

Re: One More Try

#12 Post by sickpig »

jibberjabber wrote:Why are people still answering this fool.

It's bviously a bot or a really bored troll.
like u

User avatar
Head_on_a_Stick
Posts: 13450
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: One More Try

#13 Post by Head_on_a_Stick »

sickpig wrote:like u
jibberjabber is a bot, yes. But it uses it's powers for good rather than evil.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support

theblueplll
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

#14 Post by theblueplll »

Head_on_a_Stick wrote:
sickpig wrote:like u
jibberjabber is a bot, yes. But it uses it's powers for good rather than evil.
And makes it look like he is 2 different accounts since I posted that on thread from someone else.

User avatar
sickpig
Posts: 589
Joined: 2019-01-23 10:34

Re: One More Try

#15 Post by sickpig »

Head_on_a_Stick wrote:
sickpig wrote:like u
jibberjabber is a bot, yes. But it uses it's powers for good rather than evil.
i have analysed jib's posts and the pattern confirms it not being a bot. Most likely its controlled by an erstwhile spamhunter.

User avatar
Head_on_a_Stick
Posts: 13450
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: One More Try

#16 Post by Head_on_a_Stick »

sickpig wrote:Most likely its controlled by an erstwhile spamhunter.
Sssh! Ixnay on the arryGay :mrgreen:
Black Lives Matter

Debian buster-backports ISO image: for new hardware support

User avatar
sickpig
Posts: 589
Joined: 2019-01-23 10:34

Re: To IF or not to IF

#17 Post by sickpig »

what 'sherlock holmes Dancing Men' kind of puzzle is this? i cant decrypt it

Post Reply