How can I use environment variable in systemd timer unit? - timer

I have a systemd service to execute some shell.
I want to start this systemd service with a systemd timer.
I need to make the value for OnCalendar configurable for devops.
My first attempt was to provide a config file with key TIMER_ONCALENDAR,
load it as EnvironmentFile within timer unit and set default within timer unit in case the config file doesn't provide this key.
[Timer]
Environment="TIMER_ONCALENDAR=*-*-* *:00,15,30,45:00"
EnvironmentFile=/etc/sysconfig/my-config-file
OnBootSec=2min
OnCalendar=${TIMER_ONCALENDAR}
Unit=myservice.service
[Install]
WantedBy=multi-user.target
My problem is, that the service is only run once and not every 15 min.
Is it even possible to use environment variable within timer unit and how?

If you aren't sure about a directive, like Environment=, you can use man systemd.directives to find which man page the directive is documented.
In this case, it's documented in man systemd.exec, which explains that Environment= works in 4 types of unit files, but "timer" files are not one of them.
From reading 'man systemd.timer', you can find there's no mention of environment variables for systemd timers.
But you mentioned your end goal was to help with DevOps automation. That's possible.
Read about "drop-in" files in man systemd.unit. You can create a file which contains *only* theOnCalendar` directive, which will override or add to another base configuration file.
DevOps folks can easily automate adding or replacing the file that contains the OnSchedule= directive.

Related

How to get generated content in Apache module and save it to file

I have been playing around with Apache module development and got the module working. However, I ran into the issue of where to hook properly to get all the data I need.
I am making a simple caching module that needs to hook at the beginning of the request and check if the file for this URL exists on disk and if it does then serve that file and stop content generation of Apache.
Currently, the module still continues to go into content generation mode. Let's say I have a long-running PHP script that takes 5s to generate. I would to omit calling the script altogether and just serve the static file from disk.
Furthermore, if the local file does not exist, I would like Apache to execute content generation (actually executes the PHP script) and before sending that data to the client I would like to have a proper hook that somehow gets this data and saves it to a local file.
I have tried ap_hook_fixups, ap_hook_handler and APR_HOOK_LAST, APR_HOOK_LAST and all the variations but no luck.
It always executes at the start of the request.
I also do not want to use any existing Apache modules. I want this to be a self-contained module.
Is there a way to do this kind of thing?
Based on the information you have provided, it sounds like you want your module to execute First and not last.
From what I understand of your issue, you want to make sure the file that would potentially be generated is already on disk or not, and if it is, serve that file, instead of allowing your php script to serve it.
In this case, you'll want to use APR_HOOK_FIRST or APR_HOOK_REALLY_FIRST
Then, assuming your file is on disk, you serve your file, and at the end of your module's work do a return OK;
If the file does not exist, do a return DECLINED
The DECLINED tells Apache that your module should not be the handler for this request and will continue down the list of modules till it finds something that will.
The goal here is to get your module to run before the php module does, to prevent your generation code from running, and fall back onto the php module if the requested file was not found.
Note:
The APR_HOOK_? priorities are just numbers -10 to 30
You should be able to fine tune this if you find your module executing a little too soon, like before mod_ssl for example.
Also, I am terrible at following documents, but the official Apache module development docs are amazingly assembled. Please try to use them, if you have,
I have spent the last 6 months messing around with Apache module development, working on a telegram bot.
I have had to do this song and dace a few times now.

Is it possible to change process function code(behavior) in the running time?

I just want to my flink application as much as configurable. And also i want to change the behavior of the process function in the running time instead of stopping the cluster and re-deploy the jar file.
Is there any document for that? Or is it possible to inject process function code into running jar. For instance, from the web ui, i will get the process function input(as a java code) then after submitting the form, I will update the process function behavior.
You can use a BroadcastProcessFunction (or a KeyedBroadcastProcessFunction), and on the broadcast channel, communicate (in some fashion) what the process function is supposed to do.
I've seen this technique used to broadcast javascript code (to be executed by Rhino), commands in a DSL, references to a JAR file to load, etc.
It's old and not well documented, but https://github.com/alpinegizmo/flink-training-exercises/blob/master/src/main/java/com/ververica/flinktraining/solutions/datastream_java/broadcast/TaxiQuerySolution.java is an example of this approach that uses Janino to compile and execute dynamically supplied Java expressions.

Can systemd restart a service when a specific binary on the server is updated?

On Ubuntu 18.04 I have Unattended Upgrades update apps regularly including a 3rd party PPA that installs a binary /usr/bin/some_app. My systemd unit file runs that service via ExecStart=/usr/bin/some_app. I can verify the updates work on schedule in /var/log/apt/history.log.
However even when the binary is updated via Unattended Upgrades systemd doesn't restart the app, I assume because some_app is started via a custom unit file unrelated to that PPA. So from the cli some_app --version shows v2.0.0 but systemd is still running v1.0.0.
Does systemd have a method to track a file or detect the binary referenced in ExecStart has changed on disk and it should restart? A backup hack for me would be using RuntimeMaxSec= which would get the job done, but I was hoping something more elegant existed
You could try adding a .path unit (man systemd.path) to watch for a close() after write() change to the file, which then restarts your app service. Not tested:
/etc/systemd/system/myappwatch.path
[Unit]
Description=watch for changed file
[Path]
PathChanged=/usr/bin/some_app
#Unit=myappwatch.service
/etc/systemd/system/myappwatch.service
[Service]
ExecStart=/bin/systemctl restart myapp.service
You might be able to replace the systemctl restart with something magic like Conflicts=myapp but I'll let you experiment. You also need to enable the .path unit as usual with an appropriate WantedBy=. I'm not sure what happens if the path is a symbolic link, so perhaps you should resolve the path to the real file is that is the case.

How to initiate Aldebaran ServiceManager?

I would like to stop and start ALTactileGesture service through ServiceManager during my app. I'm using Choregraphe and python boxes. I have tried different options to initiate ServiceManager but none of them works. Is there any way of doing this?
Edit:
I have already tried self.sm = session.service('ServiceManager') but did not work.
The idea is to stop ALTactileGesture as soon as the app has started:
(1) ServiceManager.stopService('ALTactileGesture') (see this)
and start/restart ALTactileGesture before the application ends:
(2) ServiceManager.startService('ALTactileGesture')
My question is how to reach ServiceManager so I can then use (1) and (2)?
You have to understand that the word "service" actually means two different things in NAOqi. See an explanation here:
NAOqi services (also called "modules"), that expose an API and are
registered to the ServiceDirectory. You can call them with qicli,
subscribe to their signals, etc.
systemd services, that are standalone executables packaged in an
Application Package, declared in it's manifest with a tag.
These are managed by ALServiceManager, who can start and stop them
(they will have their own process). For clarity's sake, these are
called "Executables" in this doc.
The confusion between the two is increased by the fact that a common
pattern is to write an executable whose sole purpose is to run a NAOqi
service, and sometimes to identify both with the same name (e.g. both
are called “ALFuchsiaBallTracker”).
Your problem here is that the NAOqi service ALTactileGesture is run by the executable registered under the ID ALTactileGesture-serv. So you need to do
ALServiceManager.stop("ALTactileGesture-serv")
(I just tested it, it works fine)
(edit) by the way, I'm not sure that actually stopping and starting ALTactileGesture is the best way of doing what you're trying to do (it seems a bit hacky to me), but if you want to do it that way, this is how :)
Just try this in robot shell (old style proxy connection):
$ python
import naoqi
s = naoqi.ALProxy("ALServiceManager", "localhost", 9559 )
s.stopService('ALTactileGesture')
>>> False
s.startService('ALTactileGesture')
>>> False # (a bit weird, but ...)
So I think it's not completely working, but at least you can connect to the ServiceManager as requested...

Run u-boot command at startup

I have a custom board running Yocto (Jethro) and would like to run a single u-boot command, preboot. Obviously, breaking the boot sequence with space and running it manually works. How do I get it to run automatically? More specifically, where is the startup command sequence, by default?
Edit: Also, I am aware I can edit the environment at runtime. However, I am trying to build this change into the image so I can distribute it.
When you are in the uboot environment. Enter printenv, it will list the environment variables that uboot uses.
There is a variable name bootcmd. Currently, mine contain a bunch of if else command. Similarly, add your prefer function there for boot.
And after it is finished and tested. Use saveenv to store the edit
Here is a syntax for uboot.
Edit:
U-Boot allows to store commands or command sequences in a plain text file. Using the mkimage tool you can then convert this file into a script image which can be executed using U-Boot's autoscr command. U-boot Scripting Capabilities
Typically, your U-Boot recipe will build U-Boot for a single machine, in that case, I'd normally just patch the compiled in, default, U-Boot environment to do the right thing. This is achieved by
SRC_URI_machine += "file://mydefenv.patch"
Or (even better) use your own git tree. This would also have the additional benefit that your system might be able to boot up and to something useful, even if the environment would be totally corrupted.
Another possibility is to do it like Charles suggested in a comment to another answer, create an environment offline, and have U-Boot load it, see denx.de/wiki/view/DULG/UBootScripts
A third possibility, that I've also used sometimes, is to construct the environment offline (possibly using the same or a similar mechanism as in the link above), and the flash the environment to flash during the normal flash programming process. Though, most of the time I've done this on AT91's, using a tcl script similar to at91 Sam-Ba TCL script
No matter which method you chose, the bootcmd variable in U-Boot should hold your boot script.
The general answer is that bootcmd is run by default, and if there is persistent environment you can change the command and 'saveenv' so that it's kept.
It is easiest to modify the said bootcmd, which is executed anyway.
As an alternative to patching the kernel, it is possible to override the command in u-boot.
Create a file e.g. platform-top.h at the same place where you would place the patch file (it might already exist) and override the CONFIG_BOOTCOMMAND.
The result will look something like this:
/* ... */
/* replace the memory write with any other valid command */
#define CONFIG_BOOTCOMMAND "mw 0x1 0x1 && run default_bootcommand"
Don't forget to make the file known in your bbapend SRC_URI = "file://platform-top.h"

Resources