Hi all,
I am trying to configure a script as a systemd service to run first when a shutdown or reboot is called. This script execute some scp commands to copy some files to other machines. My actual defined systemd’s file is:
[Unit] Description=Remote copy some files before reboot/shutdown Before=poweroff.target halt.target shutdown.target reboot.target DefaultDependencies=no
[Service] Type=simple ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=yes
[Install] WantedBy=multi-user.target
But it doesn’t work. “remote_copy” is working when it is executed from root shell. I am using CentOS-8 fully patched release.
Any idea what am I doing wrong?
Regards, C. L. Martinez
This could be the same issue that people run into when designing cron jobs. You may only have a limited set of directories on you $PATH and other environment variables may be missing. If this is the case, ensure that you define the full path to utilies:
MYPROG="/home/carlos/myprog" $MYPROG -h
rather than
myprog -h
HTH, and BTW you can still use init scripts if it is easier.
On 22/12/2020 11:51, Carlos Lopez wrote:
Hi all,
I am trying to configure a script as a systemd service to run first when a shutdown or reboot is called. This script execute some scp commands to copy some files to other machines. My actual defined systemd’s file is:
[Unit] Description=Remote copy some files before reboot/shutdown Before=poweroff.target halt.target shutdown.target reboot.target DefaultDependencies=no
[Service] Type=simple ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=yes
[Install] WantedBy=multi-user.target
But it doesn’t work. “remote_copy” is working when it is executed from root shell. I am using CentOS-8 fully patched release.
Any idea what am I doing wrong?
Regards, C. L. Martinez _______________________________________________ CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos
On Tue, Dec 22, 2020, at 07:56, J Martin Rushton via CentOS wrote:
This could be the same issue that people run into when designing cron jobs. You may only have a limited set of directories on you $PATH
Is this a systemd limit? On one of my systems I've got 233 directories (5445 non-colon chars) in interactive shell PATH and generally things work fine.
On Tue, Dec 22, 2020, at 08:12, centos2@foxengines.net wrote:
On Tue, Dec 22, 2020, at 07:56, J Martin Rushton via CentOS wrote:
This could be the same issue that people run into when designing cron jobs. You may only have a limited set of directories on you $PATH
Is this a systemd limit? On one of my systems I've got 233 directories (5445 non-colon chars) in interactive shell PATH and generally things work fine.
I misunderstood your meaning about the PATH but I understand now. you're referring to the default path defined in the shell. The Bash man page says, "The default path is system-dependent, and is set by the administrator who installs bash.", (so the CentOS packager?), and then continues on with "A common value is ``/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin''." That covers putting the script in /usr/local/bin, but then, that might not be what's on your system. On the CentOS 7.9 system I am working with, the default is:
/usr/bin:/bin
As reported by a very simple cron job that writes PATH to a file in tmp. For cron jobs I usually use the full path to the script in the crontab then set and export the PATH in the script itself.
Hi,
On Tue, Dec 22, 2020, at 06:51, Carlos Lopez wrote:
I am trying to configure a script as a systemd service to run first when a shutdown or reboot is called. This script execute some scp commands to copy some files to other machines. My actual defined systemd’s file is:
[Unit] Description=Remote copy some files before reboot/shutdown Before=poweroff.target halt.target shutdown.target reboot.target DefaultDependencies=no
[Service] Type=simple ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=yes
[Install] WantedBy=multi-user.target
But it doesn’t work. “remote_copy” is working when it is executed from root shell. I am using CentOS-8 fully patched release.
Any idea what am I doing wrong?
I don't have a CentOS 8 machine to test on but on a CentOS 7.9, this works for me:
/etc/systemd/system/shutdown-test.service
------------------------------------------------------ [Unit] Description=Remote copy some files before reboot/shutdown
[Service] Type=oneshot ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=true
[Install] WantedBy=multi-user.target ------------------------------------------------------
the mode and contents of remote_copy:
-rwxr-xr-x 1 root root 44 Dec 22 08:23 /usr/local/bin/remote_copy
------------------------------------------------------ #!/bin/bash
date >> /tmp/remote_copy_result ------------------------------------------------------
After enabling the service and rebooting, I get a file in /tmp/remote_copy_result that contains the date.
My success is attributable not to me but to this post: https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-syst... Your unit file seems similar but when I used it, it didn't work on my CentOS 7.9 system either.
Thanks centos2 .... but regarding your example, I cannot see where you configure that this services needs to be stopped before anyone else....
On 22/12/20, 15:22, "CentOS on behalf of centos2@foxengines.net" <centos-bounces@centos.org on behalf of centos2@foxengines.net> wrote:
Hi,
On Tue, Dec 22, 2020, at 06:51, Carlos Lopez wrote: > I am trying to configure a script as a systemd service to run first when a shutdown or reboot is called. This script execute some scp commands to copy some files to other machines. My actual defined systemd’s file is: > > [Unit] > Description=Remote copy some files before reboot/shutdown > Before=poweroff.target halt.target shutdown.target reboot.target > DefaultDependencies=no > > [Service] > Type=simple > ExecStart=/bin/true > ExecStop=/usr/local/bin/remote_copy > RemainAfterExit=yes > > [Install] > WantedBy=multi-user.target > > But it doesn’t work. “remote_copy” is working when it is executed from root shell. I am using CentOS-8 fully patched release. > > Any idea what am I doing wrong?
I don't have a CentOS 8 machine to test on but on a CentOS 7.9, this works for me:
/etc/systemd/system/shutdown-test.service
------------------------------------------------------ [Unit] Description=Remote copy some files before reboot/shutdown
[Service] Type=oneshot ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=true
[Install] WantedBy=multi-user.target ------------------------------------------------------
the mode and contents of remote_copy:
-rwxr-xr-x 1 root root 44 Dec 22 08:23 /usr/local/bin/remote_copy
------------------------------------------------------ #!/bin/bash
date >> /tmp/remote_copy_result ------------------------------------------------------
After enabling the service and rebooting, I get a file in /tmp/remote_copy_result that contains the date.
My success is attributable not to me but to this post: https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-syst... Your unit file seems similar but when I used it, it didn't work on my CentOS 7.9 system either. _______________________________________________ CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos
On Tue, Dec 22, 2020, at 10:50, Carlos Lopez wrote:
Thanks centos2 .... but regarding your example, I cannot see where you configure that this services needs to be stopped before anyone else....
I conveniently overlooked that requirement. I'll have to get back to you on that...
On 22/12/20, 15:22, "CentOS on behalf of centos2@foxengines.net" <centos-bounces@centos.org on behalf of centos2@foxengines.net> wrote:
Hi, On Tue, Dec 22, 2020, at 06:51, Carlos Lopez wrote: > I am trying to configure a script as a systemd service to run first when a shutdown or reboot is called. This script execute some scp commands to copy some files to other machines. My actual defined systemd’s file is: > > [Unit] > Description=Remote copy some files before reboot/shutdown > Before=poweroff.target halt.target shutdown.target reboot.target > DefaultDependencies=no > > [Service] > Type=simple > ExecStart=/bin/true > ExecStop=/usr/local/bin/remote_copy > RemainAfterExit=yes > > [Install] > WantedBy=multi-user.target > > But it doesn’t work. “remote_copy” is working when it is executed from root shell. I am using CentOS-8 fully patched release. > > Any idea what am I doing wrong? I don't have a CentOS 8 machine to test on but on a CentOS 7.9, this works for me: /etc/systemd/system/shutdown-test.service ------------------------------------------------------ [Unit] Description=Remote copy some files before reboot/shutdown [Service] Type=oneshot ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=true [Install] WantedBy=multi-user.target ------------------------------------------------------ the mode and contents of remote_copy: -rwxr-xr-x 1 root root 44 Dec 22 08:23 /usr/local/bin/remote_copy ------------------------------------------------------ #!/bin/bash date >> /tmp/remote_copy_result ------------------------------------------------------ After enabling the service and rebooting, I get a file in /tmp/remote_copy_result that contains the date. My success is attributable not to me but to this post: https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown Your unit file seems similar but when I used it, it didn't work on my CentOS 7.9 system either. _______________________________________________ CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos
Hi Carlos,
Don't know if you ever found a solution to the problem you described below but I think I came across the concept for this that might work. I'm more of a systemd dilettante than a systemd expert but I found confirmation that this approach isn't totally hare-brained. While trying to figure out your problem, I had the idea to try to create a target, instead of a service, that runs on top of multi-user or whatever your target is, then to attach the service you want to shutdown, to the shutdown sequence of that target. If your target shuts down before the target it runs on top of, then it should take your service down first. I didn't know if the shutdown sequence would be in the correct series but according to a posting I found on on "askubuntu.com" , it should work correctly. I've tried this, and I didn't see exactly the results I wanted but I'm not confident I was doing things correctly to observe it. I've included the link to what I found on askubuntu:
https://askubuntu.com/questions/1024197/how-to-have-a-process-come-first-dur...
I'm interested if you were able to find a solution, particularly if it's a different approach than what I found.
On Tue, Dec 22, 2020, at 11:08, centos2@foxengines.net wrote:
On Tue, Dec 22, 2020, at 10:50, Carlos Lopez wrote:
Thanks centos2 .... but regarding your example, I cannot see where you configure that this services needs to be stopped before anyone else....
I conveniently overlooked that requirement. I'll have to get back to you on that...
On 22/12/20, 15:22, "CentOS on behalf of centos2@foxengines.net" <centos-bounces@centos.org on behalf of centos2@foxengines.net> wrote:
Hi, On Tue, Dec 22, 2020, at 06:51, Carlos Lopez wrote: > I am trying to configure a script as a systemd service to run first when a shutdown or reboot is called. This script execute some scp commands to copy some files to other machines. My actual defined systemd’s file is: > > [Unit] > Description=Remote copy some files before reboot/shutdown > Before=poweroff.target halt.target shutdown.target reboot.target > DefaultDependencies=no > > [Service] > Type=simple > ExecStart=/bin/true > ExecStop=/usr/local/bin/remote_copy > RemainAfterExit=yes > > [Install] > WantedBy=multi-user.target > > But it doesn’t work. “remote_copy” is working when it is executed from root shell. I am using CentOS-8 fully patched release. > > Any idea what am I doing wrong? I don't have a CentOS 8 machine to test on but on a CentOS 7.9, this works for me: /etc/systemd/system/shutdown-test.service ------------------------------------------------------ [Unit] Description=Remote copy some files before reboot/shutdown [Service] Type=oneshot ExecStart=/bin/true ExecStop=/usr/local/bin/remote_copy RemainAfterExit=true [Install] WantedBy=multi-user.target ------------------------------------------------------ the mode and contents of remote_copy: -rwxr-xr-x 1 root root 44 Dec 22 08:23 /usr/local/bin/remote_copy ------------------------------------------------------ #!/bin/bash date >> /tmp/remote_copy_result ------------------------------------------------------ After enabling the service and rebooting, I get a file in /tmp/remote_copy_result that contains the date. My success is attributable not to me but to this post: https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown Your unit file seems similar but when I used it, it didn't work on my CentOS 7.9 system either. _______________________________________________ CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos
CentOS mailing list CentOS@centos.org https://lists.centos.org/mailman/listinfo/centos