Squid Proxy – Fix .pid File Disappearing After Startup

corosyncpacemakersquidUbuntu

I have two virtual Ubuntu Servers which are equal and both have Squid Proxy on them.
The problem is that Squid on server startup creates .pid file, but after 10-20 seconds it disappears and I have to manually type:

/usr/local/squid/sbin/squid

Then it works as it should and .pid file does not disappear.
I want my Squid to start on server startup and that .pid file does not disappear.
I have tried creating init.d file /etc/init.d/run_squid

#! /bin/sh
/usr/local/squid/sbin/squid
exit0

then

update-rc.d run_squid defaults
update-rc.d run_squid enable 

And I get:

error: run_squid Default-Start contains no runlevels, aborting

I also have tried

crontab -e
@reboot /scripts/squid.sh

and nothing happens when I run it , even If I give permissions.

I need .pid to run properly so my cluster works correctly ( my cluster is made of corosync and pacemaker) ,because the problem now is that when one of nodes .pid disappears, then that node simply continues working(it simply does not proxy, but it thinks it works) , but it does not switch to the healthy one.

To conclude: I want my squid to start properly that it does not lose it's pid and nodes switch if one of them lose .pid file.

Best Answer

Solution for this problem is: We have to create systemd service, so the first thing is to create a service

vim /lib/systemd/system/squid.service

Then begins case sensitive part:

[Unit]
Description=Squid Web Proxy.
[Service]
Type=simple
PIDFile=/usr/local/squid/var/run/squid.pid
ExecStart=/usr/local/squid/sbin/squid
[Install]
WantedBy=multi-user.target

Then we have to go to the

cd /lib/systemd/system/
cp squid.service /etc/systemd/system/squid.service
chmod 644 /etc/systemd/system/squid.service

And now we are able to start squid service by typing:

systemctl start squid

And to make this service start every time when we start server we have to type

systemctl enable squid