How do I create a service for a shell script ?
Problem Statement: I had written an event in MySQL to seed the dataset. Fetching the data from the R/W database and dumping to Read Only DB for reporting purposes.
This event used to pick a single piece of data and process it via stored procedure and was running every 10min. But speed was still the issue so had to find way around to speed it up.
Solution
Query which was present in event ran on mysql console ran much faster then events. So converted the mysql query to script using below command
mysql -u user_name -pPassword db_name < /etc/scripts/filename.sql
Write below query in filename.sql
delimiter //
set @max =(select max(id) from data_changes.table_name );
set @object_id=1;
WHILE @object_id is not NULL DO
set @object_id=NULL;
select @object_id:=object_id from data_changes.table_name where ID<=@max order by id limit 1;
delete from data_changes.table_name where object_id=@object_id;
call stored_procedure_name(@object_id);
call stored_procedure_name(@object_id);
END WHILE;//
Now we have script ready, now need to setup service for which we need to go to below path
cd /etc/systemd/system
Create a new file service-name.service and paste below code
[Unit]
Description={service-name} service
After=network-online.target
[Service]
ExecStart=/etc/scripts/{script-name}.sh
[Install]
WantedBy=multi-user.target
Save the file and now you can setup infinite run service with below command
sudo service {service-name} start
To Check status sudo service {service-name} status
This service will keep running in background and no need to takecare of manually checking the events or nohup service.