Search code examples
salt-project

start a system service using cron - SaltStack


I would like to override default tmp.conf at /usr/lib/tmpfiles.d/ with /etc/tmpfiles.d/tmp.conf and run the cron job at midnight on everyday. The service need to run as systemd-tmpfiles --clean. How can I run the service at midnight, Somebody help me please?

Sample code:

tmp.conf:
  file.managed:
    - name: /etc/tmpfiles.d/tmp.conf
    - source: salt://tmp/files/tmp.conf
    - user: root
    - mode: 644
    - require:
      - user: root


run_systemd-tmpfiles:
      cron.present:
        - user: root
        - minute: 0
        - hour: 0
        - require:
          - file: tmp.conf
enable_tmp_service:
      service.running:
          - name: systemd-tmpfiles --clean
          - enable: True
          - require:
            - cron: run_systemd-tmpfiles

Solution

  • If you just want the command to run as part of a cron, you would need to have that cron.present setup to run the command.

    cron_systemd-tmpfiles:
      cron.present:
        - name: systemd-tmpfiles --clean
        - user: root
        - minute: 0
        - hour: 0
        - require:
          - file: tmp.conf
    

    If you then want to run it in this state, you can't use the tmpfile.service, you would just run the command through a cmd.run, or if you only want it run when the file.managed changes, you would use cmd.wait

    run tmpfiles:
      cmd.wait:
          - name: systemd-tmpfiles --clean
          - listen:
            - file: tmp.conf
    

    But systemd-tmpfiles.service is already run on boot if you are using systemd, so there is no reason to enable it again. And when it runs during the beginning of the boot process, it will run the same way tmpfile --clean runs.