To IF or not to IF

Need help with C, C++, perl, python, etc?

To IF or not to IF

Postby theblueplll » 2019-07-21 03:51

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.
theblueplll
 
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

Postby Head_on_a_Stick » 2019-07-21 09:49

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.
User avatar
Head_on_a_Stick
 
Posts: 10471
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: One More Try

Postby reinob » 2019-07-21 11:12

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).
reinob
 
Posts: 719
Joined: 2014-06-30 11:42

Re: One More Try

Postby jibberjabber » 2019-07-21 13:01

Why are people still answering this fool.

It's bviously a bot or a really bored troll.
jibberjabber
 
Posts: 165
Joined: 2016-01-10 16:58

Re: One More Try

Postby theblueplll » 2019-07-21 17:02

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?
theblueplll
 
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

Postby Head_on_a_Stick » 2019-07-21 19:29

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.
User avatar
Head_on_a_Stick
 
Posts: 10471
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: To IF or not to IF

Postby Dai_trying » 2019-07-21 23:25

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.
Dai_trying
 
Posts: 771
Joined: 2016-01-07 12:25

Re: To IF or not to IF

Postby theblueplll » 2019-07-22 00:46

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.
theblueplll
 
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

Postby reinob » 2019-07-22 06:30

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.
reinob
 
Posts: 719
Joined: 2014-06-30 11:42

Re: To IF or not to IF

Postby Bloom » 2019-07-22 07:50

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.
User avatar
Bloom
 
Posts: 193
Joined: 2017-11-11 12:23

Re: To IF or not to IF

Postby theblueplll » 2019-07-24 07:21

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
theblueplll
 
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

Postby sickpig » 2019-07-24 11:55

jibberjabber wrote:Why are people still answering this fool.

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

like u
User avatar
sickpig
 
Posts: 315
Joined: 2019-01-23 10:34

Re: One More Try

Postby Head_on_a_Stick » 2019-07-24 17:43

sickpig wrote:like u

jibberjabber is a bot, yes. But it uses it's powers for good rather than evil.
User avatar
Head_on_a_Stick
 
Posts: 10471
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: One More Try

Postby theblueplll » 2019-07-25 01:25

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.
theblueplll
 
Posts: 154
Joined: 2019-04-29 01:17

Re: One More Try

Postby sickpig » 2019-07-27 06:49

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
sickpig
 
Posts: 315
Joined: 2019-01-23 10:34

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable