9

I want to run a custom bash script before httpd service is started with systemd. I'm using Centos 7.

Any idea?

Kind regards.

Javi
  • 121
  • 1
  • 1
  • 2
  • create service which will run those script, and add this service as prerequisite to httpd – darvark Mar 24 '17 at 12:47
  • 4
    Show us what you've tried so far, and explain what worked, what didn't work, and what parts of the documentation you found unclear. – larsks Mar 24 '17 at 12:48

1 Answers1

24

Assuming your httpd service is named httpd.service, create a directory /etc/systemd/system/httpd.service.d; and create a file named run-my-script-first.conf (or whatever you like) within it with the following contents:

[Service]
ExecStartPre=/path/to/your/script
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 3
    @digfish, "doesn't work" is almost uselessly vague, but to take a guess -- did you run a `systemctl daemon-reload` (or reboot) to activate config file changes? If you just changed the file and immediately tried restarting your service, that would explain the new configuration not being active yet. – Charles Duffy Mar 18 '19 at 12:56
  • 1
    @digfish, ... Another thing you can try is looking at `systemctl status httpd`, to see whether it shows what happened to the ExecStartPre command during its invocation. (Obviously, that'll only have useful output after the configuration was loaded and the service was started after it). – Charles Duffy Mar 18 '19 at 13:01
  • Thanks for the reply @Charles Duffy, but I was trying do this for the `ssh` daemon, so I created creating a directory called `ssh.service.d` with the .conf file inside, but I will try to execute the `systemctl daemon-reload`. – digfish Mar 19 '19 at 18:06
  • Unfortunately, It didn't work ! Maybe for `sshd` the things are different.. – digfish Mar 19 '19 at 18:31
  • 1
    @digfish, "didn't work" is, again, pretty uselessly vague -- it doesn't distinguish between the config file not being read by systemd at all, the file being read but rejected or overridden, the config file being read and its contents being parsed but the script it triggers not working right, and other possible variants. Does `systemctl status ssh` (BTW, are you sure it's called `ssh`, not `sshd`, for your distro?) show the `ExecStartPre` service, after you've done the `daemon-reload` and then a `systemctl restart ssh`? – Charles Duffy Mar 19 '19 at 20:04
  • 1
    @digfish, ...`systemctl show ssh | grep ExecStart` is a good place to start to figure out what the active configuration is. – Charles Duffy Mar 19 '19 at 20:40