Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1266  / 3 Years ago, tue, august 24, 2021, 1:22:41

I am using upstart instance jobs to start few of my services. The jobs are meant to have one instance per user i.e they take in the user name as the instance parameter. I have also configured them to be respawned by upstart. The respawning works. Here is what my config file looks like



start on runlevel [2345]
stop on runlevel [06]

normal exit 0

respawn
respawn limit 5 300
instance $user

chdir /home/talha/syncservice/
script
exec python sync.py $user
end script


The issue is that I want these instance jobs to be restarted on system reboot. Obviously I cannot pass in all the usernames as the instance parameters on reboot as I do not know what and how many instances were spawned last time.



Is there a way that upstart can persist its state tables for instance jobs across reboots. Why I assume that there are some "state table"?, is because I assume that upstart keeps a track of all the running instance jobs. There would be a state table that tracks which instance to respawn when it crashes. Otherwise it wont be able to respawn an instance job. So if those tables can be made persistent across reboots my problem will be solved.



Can the persistence be somehow achieved.? Where does upstart keep the track of its running jobs? Is it only in memory or a file?



If that cannot be done, that means that the stanza



start on runlevel[2345]


has no meaning or benefit for instance jobs.


More From » 12.04

 Answers
5

You could save the list of users to a file (user-sync.list in this example). To restore the user scripts you could use a for loop in a bash script that's run by root at boot. Where your init script is called user-sync:



#!/bin/sh
# /root/restore-user-sync.sh
for user in `cat user-sync.list`; do start user-sync user=$user; done


Add that to crontab, as root:



$ crontab -e


In the cron file:



@reboot /root/restore-user-sync.sh


Maintaining the list of active users is the most involved part. You could have the python script do that, or you could do it as part of the Upstart script:



# /etc/init/user-sync.conf

start on runlevel [2345]
stop on runlevel [06]

normal exit 0

respawn
respawn limit 5 300
instance $user

chdir /home/talha/syncservice/

pre-start script
# if $user doesn't already exist in list, add $user to list
if ! grep $user user-sync.list; then echo $user >> user-sync.list; fi
end script

script
exec python sync.py $user
end script

pre-stop script
# remove line(s) from list that exactly match $user
sed -i "/$user/d" user-sync.list
end script


You might be able to substitute $USER (which is automatically defined as the current username) for $user, otherwise when you call the upstart script you'll need to pass the username as a parameter:



sudo start user-sync user=myusername

[#27142] Tuesday, August 24, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zzlingots

Total Points: 414
Total Questions: 130
Total Answers: 117

Location: Sudan
Member since Tue, Sep 15, 2020
4 Years ago
;