Re: shell script run in backend

2023-05-14 Thread Greg Wooledge
On Sun, May 14, 2023 at 04:36:03PM +, Andy Smith wrote:
> On Sun, May 14, 2023 at 05:04:50PM +0800, Tom Reed wrote:
> > I know convert it to a perl script and run it under App::Daemon for
> > background jobs.
> 
> Having it as a systemd service is a much cleaner solution, whether
> it is shell or Perl or any other language.
> 
> The main point of the App::Daemon module is to detach from shell,
> redirect stdout etc and provide start/stop/status commands, all of
> which are provided by systemd.
> 
> I suppose if aiming for it to be portable to other init systems it
> could still be useful, but even so if on a machine with systemd I'd
> run it as a systemd service and tell App::Daemon to run it
> foreground (so that systemd takes care of backgrounding it).

Or, simply don't use App::Daemon at all.  Just let your program work
in a normal way -- as a single process, with no funky gymnastics.
That's the easiest thing to handle.  It's the correct way.



Re: shell script run in backend

2023-05-14 Thread Andy Smith
Hello,

On Sun, May 14, 2023 at 05:04:50PM +0800, Tom Reed wrote:
> I know convert it to a perl script and run it under App::Daemon for
> background jobs.

Having it as a systemd service is a much cleaner solution, whether
it is shell or Perl or any other language.

The main point of the App::Daemon module is to detach from shell,
redirect stdout etc and provide start/stop/status commands, all of
which are provided by systemd.

I suppose if aiming for it to be portable to other init systems it
could still be useful, but even so if on a machine with systemd I'd
run it as a systemd service and tell App::Daemon to run it
foreground (so that systemd takes care of backgrounding it).

Cheers,
Andy

-- 
https://bitfolk.com/ -- No-nonsense VPS hosting



Re: shell script run in backend

2023-05-14 Thread Tom Reed
stem service as Jeremy Ardley suggests in a different reply.
>
> Exactly:
>
>   script > /tmp/script.log 2>&1 &
>
> (adjust paths to taste). For good measure, and if your shell
> has job control, it will output the job number and PID, like
> so:
>
>   [1] 15211
>
> (1 is the job number, 15211 is the PID, actual numbers will
> vary). You then issue
>
>   disown %1
>
> (assuming bash here), which lets your shell "forget" about job
> number 1 and keep it for messing around once you leave your
> shell (in some setups, terminating the shell might terminate
> the background jobs, but my memory might be fuzzy).
>

Thanks for all your helps.
I know convert it to a perl script and run it under App::Daemon for
background jobs.

regards
Tom


-- 
sent from https://dkinbox.com/



Re: shell script run in backend

2023-05-14 Thread tomas
On Sun, May 14, 2023 at 12:20:02AM -0700, Will Mengarini wrote:
> * Tom Reed  [23-05/14=Sun 14:21 +0800]:
> > I have a long run shell script [...].  Currently the script
> > is running in front-end in shell.  How can I run it with
> > the backend way? Can I register it as a system service?
> 
> Just run 'myScript&' (the trailing '&' tells the shell to
> run it in the background) if there is no terminal output
> from the running script (terminal output will pause it); if
> there is, enclose the content of the script in redirection of
> standard output and standard error to a log file, or code a
> system service as Jeremy Ardley suggests in a different reply.

Exactly:

  script > /tmp/script.log 2>&1 &

(adjust paths to taste). For good measure, and if your shell
has job control, it will output the job number and PID, like
so:

  [1] 15211

(1 is the job number, 15211 is the PID, actual numbers will
vary). You then issue

  disown %1

(assuming bash here), which lets your shell "forget" about job
number 1 and keep it for messing around once you leave your
shell (in some setups, terminating the shell might terminate
the background jobs, but my memory might be fuzzy).

Cheers
-- 
t


signature.asc
Description: PGP signature


Re: shell script run in backend

2023-05-14 Thread Will Mengarini
* Tom Reed  [23-05/14=Sun 14:21 +0800]:
> I have a long run shell script [...].  Currently the script
> is running in front-end in shell.  How can I run it with
> the backend way? Can I register it as a system service?

Just run 'myScript&' (the trailing '&' tells the shell to
run it in the background) if there is no terminal output
from the running script (terminal output will pause it); if
there is, enclose the content of the script in redirection of
standard output and standard error to a log file, or code a
system service as Jeremy Ardley suggests in a different reply.



Re: shell script run in backend

2023-05-14 Thread Jeremy Ardley



On 14/5/23 14:21, Tom Reed wrote:

Currently the script is running in front-end in shell.
How can I run it with the backend way? can I register it as a system service?


sudo nano /etc/systemd/system/myscript.service

[Unit]
Description=My Script

[Service]
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload
sudo systemctl enable myscript.service

sudo systemctl start myscript.service

sudo systemctl status myscript.service

--
Jeremy
(Lists)