Times change and so do best practices.
The current best way to do this is to run systemctl edit myservice
, which will create an override file for you or let you edit an existing one.
In normal installations this will create a directory /etc/systemd/system/myservice.service.d
, and inside that directory create a file whose name ends in .conf
(typically, override.conf
), and in this file you can add to or override any part of the unit shipped by the distribution.
For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf
:
[Service]
Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"
Also note that if the directory exists and is empty, your service will be disabled! If you don't intend to put something in the directory, ensure that it does not exist.
For reference, the old way was:
The recommended way to do this is to create a file /etc/sysconfig/myservice
which contains your variables, and then load them with EnvironmentFile
.
For complete details, see Fedora's documentation on how to write a systemd script.
You can't do that as a normal user. But you can setup a cgroup as root, and make it configurable by your user.
If you do not already have the default cgroups controllers mounted by e.g. systemd:
$ sudo mount -t tmpfs cgroup_root /sys/fs/cgroup
$ sudo mkdir /sys/fs/cgroup/cpuset
$ sudo mount -t cgroup -o cpuset cpuset /sys/fs/cgroup/cpuset
Create a cgroup:
$ sudo mkdir /sys/fs/cgroup/cpuset/${USER}
$ sudo chown -R ${USER} /sys/fs/cgroup/cpuset/${USER}
You can now modify the configuration of your cgroup as a normal user:
$ echo 0-3 > /sys/fs/cgroup/cpuset/${USER}/cpuset.cpus
Add a process to that group:
$ ./my_task &
$ echo $! > /sys/fs/cgroup/cpuset/${USER}/tasks
Or create a subgroup:
$ mkdir /sys/fs/cgroup/cpuset/${USER}/subgroup
$ echo 0-1 > /sys/fs/cgroup/cpuset/${USER}/subgroup/cpuset.cpus
$ ./my_other_task &
$ echo $! > /sys/fs/cgroup/cpuset/${USER}/subgroup/tasks
Best Answer
If you want to do this for ALL cgroups, do you want to do this for the whole system? If that's the case, you can set swappiness system wide in "/etc/sysctl.conf". The line you would be editing is "vm.swappiness=" which you can set to anything from 0 to 100.
As a fair warning to anyone moving this value as a way to avoid swapping altogether, setting this very low will almost entirely disable demand paging, which is a very useful way to get idle pages out of RAM and into swap. Yes, it's "swapping", but that's not such a desperate thing as is the old traditional sense of swapping when we run out of memory. Demand paging is especially useful in large databases, and it shouldn't really slow anything down. It certainly beats swapping things out when we're out of memory last second, as demand paging helps to keep that from happening in the first place while the system isn't grinding to a halt, rather than after it is. A setting of 0 for vm.swappiness system wide can cause a system to OOM kill processes when under heavy pressure, potentially bringing the whole system down in a kernel panic.
Also worth mentioning, you must call upon the sysctl.conf file to be read after editing it.
# sysctl -p
reads lines that have been modified in /etc/sysctl.conf. vm.swappiness can be changed on a running system this way, or by using procfs thusly:# echo 50 > /proc/sys/vm/swappiness
where 50 is any number from 0 to 100.