[Solved] Launching multiple instances of a service from systemd timer

I have a short script that I want to run every 5 seconds.  I've set up a systemd timer, which runs the script every 5 seconds, assuming the script terminates before the next elapse of the timer.  However, if the script is still running when the timer elapses, systemd sees that the service is already active, so it does not run my script again. 
How can I make my script run in another instance, every 5 seconds, even in cases where the script is still running from an earlier elapse of the timer? 
I'd like to keep everything managed by systemd, in that it should capture stdout into the journal.  I've looked at using a systemd socket unit with ListenStream pointing to a file system socket, where the socket-activated service template runs my script.  Then, a timer unit runs a script that calls socat to send a single character to the file system socket to spin up an instance.  The nice thing about this approach is that systemd can manage limiting the number of socket activations that can be spun up.  However, I haven't gotten this idea to work yet. 
Here's my test script, which I can cause to run for a long time by creating a lockfile. 
[root@dogbert system]# cat /var/lib/misc/test1.py
#!/usr/bin/python2
import os
import time
import sys
print "%d starting..." % os.getpid()
sys.stdout.flush()
while os.path.exists("/tmp/test1.lockfile"):
print "%d sleeping..." % os.getpid()
sys.stdout.flush()
time.sleep(1.0)
print "%d exiting..." % os.getpid()
Here are my unit files:
[root@dogbert system]# cat /etc/systemd/system/test1.timer
[Timer]
OnBootSec=1min
OnCalendar=*:*:0/5
AccuracySec=50ms
[Install]
WantedBy=timers.target
[root@dogbert system]# cat /etc/systemd/system/test1.service
[Service]
Type=oneshot
ExecStart=/var/lib/misc/test1.py
Here's what happens if I create the lock file and then remove it.  The script does not run at 10:39:30, but I get one "catch-up" run of the script at 10:39:32. 
May 02 10:39:20 dogbert test1.py[10209]: 10209 starting...
May 02 10:39:20 dogbert test1.py[10209]: 10209 exiting...
May 02 10:39:25 dogbert test1.py[10228]: 10228 starting...
May 02 10:39:25 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:26 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:27 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:28 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:29 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:30 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:31 dogbert test1.py[10228]: 10228 sleeping...
May 02 10:39:32 dogbert test1.py[10228]: 10228 exiting...
May 02 10:39:32 dogbert test1.py[10250]: 10250 starting...
May 02 10:39:32 dogbert test1.py[10250]: 10250 exiting...
May 02 10:39:35 dogbert test1.py[10269]: 10269 starting...
May 02 10:39:35 dogbert test1.py[10269]: 10269 exiting...
May 02 10:39:40 dogbert test1.py[10287]: 10287 starting...
May 02 10:39:40 dogbert test1.py[10287]: 10287 exiting...
Last edited by johnp636 (2015-05-05 01:22:35)

johnp636 wrote:However, if the script is still running when the timer elapses, systemd sees that the service is already active, so it does not run my script again.
That's normal, you cannot start again an already active service unit, which is what the timer tries to do if the next trigger comes before the service ends.
How can I make my script run in another instance, every 5 seconds, even in cases where the script is still running from an earlier elapse of the timer?
I don't think that a timer can do this with a simple service.
But a shell script can manage this.
I'd like to keep everything managed by systemd, in that it should capture stdout into the journal.
The command 'systemd-cat' can be used in the shell script to get this feature.
I propose this bash script:
#!/bin/bash
nbprocess=0
while true; do
if ((nbprocess <= 2)); then
systemd-cat /var/lib/misc/test1.py &
fi
nbprocess=$(pgrep test1.py|wc -l)
sleep 5
done
It limits the number of test1.py processes to 3.
This script can be started with a service unit, without using a timer:
[Service]
ExecStart=/var/lib/misc/test1.sh
Stopping this service, will end all the running test1.py processes.
Maybe there is a better way to do this, but this method is simple; if I find something else, I will post again.

Similar Messages

  • Multiple instances of a service from a bean

    Hi all,
    I have a class that provides a service, however there can multiple instances of this object and I need to retrieve all instances of the service. A simplified version of my function is:
    public MyService getMyService() throws Exception
    if (_myService == null)
    myService = (MyService)getBeanContextServices().getService(bccs, this, myService.class, null, new MyServiceRevokedListener());
    return _myService;
    I guess that I need to return a Collection of MyService objects from this function. When I call getService(...) there is only on instance returned (not surprising really). I have been searching through the documentation for a function that will return all instances of a service. This sounds like functionality that I would have expected to be provided by the API so before I write application specific code, I was wondering if anyone out there could offer some advice.
    Any thoughts and suggestions would be appreciated.
    Regards,
    Chris

    You mean likepublic MyService[] getMyServices()...? If you're looking for an API that returns a set of resources, try java.awt.GraphicsEnvironment.

  • Upon computer startup: "Atte​mpted to start multiple instances of the service&qu​ot;

    Recently installed Diadem and there must be some sort of registry error.  Whenever I startup I get the error "Attempted to start multiple instances of the service" anyone know the registry fix for this? Thanks!
    Attachments:
    NI startup error.jpg ‏16 KB

    Hey Kyle,
    Check your services and see what’s running.  This can be done by going to Start>>Run and type “services.msc”.   Then look for anything that starts with National Instruments or NI.  Look to see if there are any entries running twice or if you are trying to call a service that is already running.
    Nick
    National Instruments
    Applications Engineer

  • "you can not use multiple instance of Program at the same time"

    Hello @ all,
    I have a Problem, after the installing from Acrobat Testversion and acrobat reader in version in "all Combination" 8 an 9.
    I get this misstake information "you can not use multiple instance of Program at the same time" if I want open more then 1 .pdf.
    If I have no pdf open, but acrobat/reader so i can´t open with the same Misstake.
    I have deinstalled and installed, Allways I bekame the same information. I tryed all things to clean and know i don`t know forward.
    Important: by Drag an Drop in the Acrobat Reader it is going, but by direct click on the pdf i get mistake.
    Maybe, you know what I can do!
    Thanks for help!

    Installation of both Acrobat and Reader in the same Windows machine is not advisable. If I were you, I would remove both completely and then reinstall the one you want to keep.

  • I want to launch multiple instances of JVM

    hi guys,
    When I go to a games room, the applet starts up and then a JVM is launched and it is visible in taskbar. If I click on another game room, the virtual machine launches another applet and the first one is destroyed, but thats no good to me, cos I want to have both applets running at the same time. I can achieve this by running one room from IE and another from Firefox, but is there a way to launch all from IE only, cos if I run both Firefox and IE it starts screwing up with my machine. So basically, I want to be able to run many instances of JVM.
    Thanks.

    That's odd, because one instance of the JVM should be
    able to handle multiple instances even of the same
    applet. Are you opening them in separate windows?no, in the same browser window...what happens is that when you click on a room, the previous window in whivh the applet was running is destrpyed and a new one is created

  • How to launch multiple instances of browser

    I use multiple browser instances at once; but, I don't see how to do in on the MacPro.

    Hello,
    If you want multiple browser windows open at the same time (such as in Safari), then you go to the browsers file menu and choose "New Window"
    If you have tabbed browsing enabled, then you can choose "New Tab" from the same menu, and then just create a second, third, etc. Tab to view web pages in.
    You can enabled Tabbed Browsing in Safari by going to the Safari Menu, then Preferences, then the "Tabs" tab. Then, enable Tabbed browsing (set the other tab preferences as you like).
    I hope this helps.
    Let us know if you have other questions.
    P.S., if you'd like, go ahead and click the "Helpful" or "Solved" buttons on any of the posts / replies above if you feel they were helpful or adequately answered your question.

  • Running multiple instances of a service

    ello,
    is it possible (and not "tabooed") to run multiple instances of an application that is designed to be service?
    Im intending to use the Java Service Wrapper to have my app run as a service, but im wondering if it is possible to run multiple instances of my application wrapped in the wrapper.
    What about starting the service using command line arguments?
    This is some details of my scenario:
    Im developing a server that will run without threading - it will read client requests that have been saved by the clients into a database. Thus im going to have an infinite while loop that just keeps reading the db for unprocessed client requests.
    When it is noticed that the load has increased then more than one instance of the server will be necessary.
    if im using the java service wrapper to run my server as a service will it be possible to achieve the above?
    thanks.

    one more thing : if i make any changes in the wrapper.conf xml file for the java service wrapper will the config be automatically loaded?

  • Attempted to start multiple instances of the service

    After installing LabVIEW 8.5.1, I keep getting this message after a computer reboot.  It does not say what multiple services are running.  Anyone seen this before?

    SumTumWong-
    Check your services and see what’s running.  This can be done by going to Start>>Run and type “services.msc”.   Then look for anything that starts with National Instruments or NI.  Look to see if there are any entries running twice or if you are trying to call a service that is already running.
    Regards,
    Mike S
    NI AE

  • [Solved] Launch GUI application and detach it from terminal

    Hello, I find it convenient launching apps from terminal, but after that I am unable to use the terminal, since it it's "attached" to the process. Some searching revealed 2 methods of doing this, but they both don't work for me
    1) add "&" after the command: upon closing the terminal the application closes as well; not what I want
    2) press ctrl+z: this just closes the app
    So how do I type a command to launch a GUI app and continue using the terminal?
    I am using mate-terminal.
    Last edited by axper (2013-02-11 19:14:08)

    kaszak696 wrote:https://bbs.archlinux.org/viewtopic.php?id=157405
    Thanks a lot, this is exactly what I wanted (for Bash):
    olive wrote:
    You can disown them (man bash). I have put
    PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'disown -a -h'
    in my ~/.bashrc with the effect that if you launch a program in the background (with &). It will not be stopped when you close the terminal.
    For Zsh, put this in your .zshrc
    precmd_disown() {
    emulate -L zsh
    setopt extendedglob
    local job match mbegin mend
    jobs | while read job; do
    if [[ $job = \[(#b)([[:digit:]]##)\]*running* ]]; then
    disown %$match[1]
    fi
    done
    autoload -U add-zsh-hook
    add-zsh-hook precmd precmd_disown
    (from http://www.zsh.org/mla/users/2010/msg00654.html)
    Last edited by axper (2013-06-29 07:47:21)

  • I want to launch multiple scripts written in Java from command line

    Hi,
    I have created a Master script file (script.java) which call functions available in other class files. (created through Java code).
    Now, let's say I have 10 functions(scripts) on master script file but I want random selection for executing these functions. Do we have any interface for selection from available scripts?
    Edited by: 917140 on Apr 10, 2012 8:11 AM

    "Everything I know about the command is in the man page. I can't give you a literal command, because I don't know what to put in for the arguments. To be honest, if the man page is not understandable enough for you to build the command, I don't think you should use it at all. Use Profile Manager in OS X Server, which is the right way to do what you're trying to do."
    I do not know why this post did not show up here on the thread, I did get the email. Not sure how to respond. We have 800 stations. Can not go and touch each one, so looking for a command line to remove the ssid "campus" - If I read your message correct, you do not know the command line either for this. Thank you.

  • Why is adobe acrobat launching multiple instances on my computer with each using ~ 30% of cpu

    Right now I have to cancell multiple processes every 30 minutes.  My computer is running @ 100% constantly with the fan blowing.  The system is a  Core i7 laptop running windows 8.
    i just killed 2 processes and another one popped right back up -- consuming 32% of my processor.

    You'd better follow this thread: http://forums.adobe.com/message/5329751#5329751
    You're not alone in this case ;-)

  • Hacking iPhoto, running multiple instances of iPhoto at the same time.

    Inspired by Locking Photo Album ... I'm looking for a way to open two iPhoto library folders simultaneously.
    1. Copy iPhoto
    2. edit /Applications/iPhoto.app/Contents/Info.plist
    CFBundleIdentifier com.apple.The Second iPhototo obtain a second preference file
    3. Configure iPhoto 1 to use the first library and iPhoto 2 accordingly.
    Unfortunately, iPhoto prevents you from launching two apps at the same time:
    Caution: iPhoto is already running, Only one copy can be running at a time.
    So how can we disable that kind of limitation?
    Launching /Applications/iPhoto.app/Contents/MacOS/iPhoto via Terminal doesn't work either.
    Thanks in advance!

    well it is possible, and it is the best solution, but as a side note, to get X applications running as another user you need to:
    userthatisrrunningX@myhost ~ xhost +localhost
    otheruser@myhost ~ kopete
    if you want to run something as root the prefered way to do this in kde is to use kdesu..

  • Multiple instances of same computer connected (some times)...

    So, I finally fixed (most) of the issues I was having with iCloud and email by hand tweaking my way through ~./Library/Preferences/com.apple.[etc]...
    The only weird thing that remains is that sometimes I get a dialogue box that pops up saying (not verbatim), "COMPUTERNAME is now connected to iMessage, FaceTime, etc if you have recently connected, you can ignore this.".  Sometimes it says, "COMPUTERNAME (1)" is also connected.
    The HOSTNAME is set to COMPUTERNAME only.  (Not actually, "COMPUTERNAME", but using that as example.)
    Is there a cache somewhere that could be causing this "COMPUTERNAME (1)" to randomly pop up at times?
    Sorry that I'm not being verbatim, but I'm  really lazy to get the exact wording at the moment.
    I just started using Mac's (love 'em), but I have several decades with PC's, including with GNU/Linux..
    Note: If it makes any difference, I am running Yosemite (Beta) on this particular Mac. 
    Thanks!

    Just got off the phone with Apple Support and they said this is a known issue and will be addressed in future updates

  • Multiple Instances created for a web service in OWSM

    Hi,
    I have created a gateway and added some services to the gateway. When i am invoking the webservice through the gateway, multiple instances of the service are created in bpel console.
    I have done everything as per the OWSM guide.
    Urgent Issue.
    Thanx in advance.

    Dear Nikhil,
    Please check that item is relevant for Text item,
    Check the copy control settings between sales order type and delivery type in VTLA transaction.
    I hope it will help you,
    Regards,
    Murali.

  • Indesign Server CS5 Multiple instance - Urgent, Please help!

    Hi,
    I am using a windows service to access the indesign server. The instance is created like below.
            Dim oType As Type = Type.GetTypeFromProgID("InDesignServer.Application")
            If oType IsNot Nothing Then
                     Return Activator.CreateInstance(oType)
            End If
    If already an instance is created by some other application, then my service is not able to create an instance. I get the following error from the COM
    "Retrieving the COM class factory for component with CLSID {96D10A00-63E3-423E-A223-DFC4BEA21206} failed due to the following error: 80080005."
    In some article i read that instance can be created by different port numbers and different configuration.
    Is there anyway in VB.net to create an instance like that and will it solve the multiple instance problem ?
    Regards,
    Karthik B

    The solution on that site looks pretty good in theory.
    What did you do to implement it?
    Did you return a forward page from your executeSynchro method which you overrode to implement it?

Maybe you are looking for

  • I, and my clients, want a VERY simple contact form hosting option...

    There's no need for us to have data collected and analyzed. No need for yet another hosting service to store data we'll NEVER use. I've already got a hosting service for my site. My clients already have hosting for their sites. None of us are willing

  • Syncing apps from my iPod to new iTunes library

    I recently went through a hard drive failure on my laptop and was unable to back up my iTunes library. I lost everything. Now I reinstalled iTunes on my new hard drive, plugged my iPod in and clicked Transfer Purchases. All my songs and movies got tr

  • Retrieving size of DAM Asset

    I'm trying to get the size (in this case it's 30 KB but it could be whatever) of an asset from the CQ5 DAM. I've written code like this below that gives me the title Resource rsc = resourceResolver.getResource(damLink +"/jcr:content/metadata");      

  • Material ledger:'not allocated' variance

    Dear all, For raw material A, it issued to certain production order(say FG is B) in Apr. at the month end, I found that there are some variance(not distributed) in material A due to there is no goods receipt for production order. in May. I performed

  • Stacked Bar graph in ABAP

    Hi, Can I create a Stacked Bar graph using ABAP Function Modules? If yes, can anybody please explain how with any suitable example. I also need different colors in each bar of the graph. Any help would be appreciated. Thanks.