LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2019, 08:45 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
How to start and stop service in a script?


Hello

I've got this service jackett.service which I want only to start when the qbittorrent app is started and stopped when the qbittorrent app is closed.

So I wrote this small script and linked it to .desktop file.
Code:
# cat /usr/local/bin/qbit.sh
#!/bin/bash
sudo systemctl start jackett
/usr/local/bin/qbittorrent
sudo systemctl stop jackett

# cat /usr/local/share/applications/qBit.desktop 
[Desktop Entry]
Categories=Network;FileTransfer;P2P;Qt;
Exec=/usr/local/bin/qbit.sh
GenericName=BitTorrent client
Comment=Download and share files over BitTorrent
Icon=qbittorrent
MimeType=application/x-bittorrent;x-scheme-handler/magnet;
Name=qBits
Terminal=false
Type=Application
StartupNotify=false
StartupWMClass=qbittorrent
Keywords=bittorrent;torrent;magnet;download;p2p;
The problem I'm facing is when I run the script in terminal, it works right.

Code:
$ sudo systemctl is-active jackett.service
unknown

$ qbit.sh &
[1] 25326

$ sudo systemctl is-active jackett.service
active

And finally when qBittorrent app is closed by mouse.

$ sudo systemctl is-active jackett.service
unknown
But when I execute qBit.desktop, jackett.service is not started.

Code:
$ sudo systemctl is-active jackett.service
unknown

Now qBit app is opened by clicking its icon in menu. Still jackett.service is not started.

$ sudo systemctl is-active jackett.service
unknown
Any solution is really appreciated. Thanks

Last edited by ddenial; 04-26-2019 at 08:51 AM.
 
Old 04-26-2019, 11:06 AM   #2
lordmax10
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Rep: Reputation: Disabled
I haven't undestood

Why always sudo?

your user can't start services?


I've always used something like this:

Quote:
#!/bin/bash
service=replace_me_with_a_valid_service

if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
/etc/init.d/$service start
fi
 
Old 04-26-2019, 04:16 PM   #3
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
When you run it from the terminal you are authenticating to sudo before calling the script in the line
Code:
sudo systemctl is-active jackett.service
I am not sure how this behaves in the execution environment of the .desktop file.

You may have to enable password-less execution for the script and call it with sudo.

EDIT: If you do play with password-less execution make sure that you are not running the graphical application as root unintentionally.
I also just had one other thought: I have no idea what jackett is, but you might be able to run it as a regular user if you install the service file in your home directory and start the service with the --user option.
See e.g. here for more info: https://wiki.archlinux.org/index.php/Systemd/User

Last edited by joe_2000; 04-26-2019 at 04:32 PM. Reason: added info on system user service
 
2 members found this post helpful.
Old 04-26-2019, 04:17 PM   #4
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by lordmax10 View Post
I haven't undestood

Why always sudo?

your user can't start services?


I've always used something like this:
Starting and stopping services under systemd with sudo is perfectly correct.
The snippet you posted calls a sysv init script.
 
Old 04-26-2019, 05:10 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Another observation: running from the terminal, you are running your script in background. Not the case when run from the .desktop I’m not sure what thar means, but it is a difference.
 
Old 04-27-2019, 08:07 AM   #6
lordmax10
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
Starting and stopping services under systemd with sudo is perfectly correct.
Yes I know
My question is? It's necessary?

Quote:
Originally Posted by joe_2000 View Post
The snippet you posted calls a sysv init script.
Yes I know, and normallly it's sufficient
 
Old 04-29-2019, 02:39 AM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by lordmax10 View Post
Yes I know
My question is? It's necessary?
It depends on the service. As stated above I do not know what jackett does. If the process can run with normal user privileges it might not be necessary, but you then have to setup the service as a user service as described in the arch wiki article I linked to in a previous post.


Quote:
Originally Posted by lordmax10 View Post
Yes I know, and normallly it's sufficient
The /etc/init.d calls should be considered as deprecated under systemd systems. There is a compatibility layer in Debian (and presumbly its *buntu derivatives), but I wouldn't recommend to use it unless you have good reasons.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script that call another stop start script and use paarmeter stop,start ,status rcroyal88 Programming 5 07-21-2018 01:40 AM
I need a service to stop or start rc.inet wla2_start(stop) igadoter Slackware 1 04-21-2017 03:12 PM
[SOLVED] I can stop a service, but not start the same service from php? jesse.gibson Linux - Software 3 02-24-2016 01:07 PM
puppet needs to stop a service, do something, then start the service edwardcode Programming 1 12-03-2012 01:58 PM
AWK- processing data inside start/stop pairs but ignoring start/stop lines cliffoij Programming 2 10-15-2008 06:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration