You are not logged in.

#1 2017-04-11 03:43:30

chris190
Member
Registered: 2017-04-05
Posts: 3

How to get a cron job to start @reboot?

So I have made script that will minimize all windows 10 seconds after boot. It works when I run it in a terminal... but when I add it in the crontab it completely stops working....

My crontab looks like this,

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /home/chris/test.sh

^new line at the end



And my script file is called test.sh

#! /bin/bash
sleep 10 && xdotool key super+d

What is wrong with my configuration that i'm not seeing/getting?

Thank in advance for any help!!!

edit: also this is the output of systemctl status cronie.service

cronie.service - Periodic Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2017-04-10 22:01:27 CDT; 43min ago
 Main PID: 358 (crond)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/cronie.service
           └─358 /usr/bin/crond -n

Apr 10 22:42:11 ArchLinux CROND[5251]: (chris) CMDOUT (Error: Can't open display: (null))
Apr 10 22:42:11 ArchLinux CROND[5251]: (chris) CMDOUT (Failed creating new xdo instance)
Apr 10 22:43:01 ArchLinux crond[5294]: pam_unix(crond:session): session opened for user chris by (uid=0)
Apr 10 22:43:01 ArchLinux CROND[5295]: (chris) CMD (/home/chris/test.sh)
Apr 10 22:43:11 ArchLinux CROND[5294]: (chris) CMDOUT (Error: Can't open display: (null))
Apr 10 22:43:11 ArchLinux CROND[5294]: (chris) CMDOUT (Failed creating new xdo instance)
Apr 10 22:44:01 ArchLinux crond[5342]: pam_unix(crond:session): session opened for user chris by (uid=0)
Apr 10 22:44:01 ArchLinux CROND[5343]: (chris) CMD (/home/chris/test.sh)
Apr 10 22:44:11 ArchLinux CROND[5342]: (chris) CMDOUT (Error: Can't open display: (null))
Apr 10 22:44:11 ArchLinux CROND[5342]: (chris) CMDOUT (Failed creating new xdo instance)

Last edited by chris190 (2017-04-11 04:05:21)

Offline

#2 2017-04-11 03:55:52

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: How to get a cron job to start @reboot?

The first line of the error message, cron runs in a limited environment and knows nothing about DISPLAY.

Use a systemd timer and a user service.

Also, use code tags when pasting to the boards: https://wiki.archlinux.org/index.php/Co … s_and_code


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2017-04-11 04:05:52

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,475

Re: How to get a cron job to start @reboot?

@reboot starts cron script as soon as cron starts on reboot, so your window-manager may not have started yet.
To tell cron about display use something like this

@reboot env DISPLAY=:0 <yourscript>

However still cron may start your script before your window-manager has started.
It would be better to add this script to your window-manager/DE's startup file or start it with systemd as suggested by Jasonwryan.

Last edited by Docbroke (2017-04-11 04:07:42)

Offline

#4 2017-04-11 04:31:39

chris190
Member
Registered: 2017-04-05
Posts: 3

Re: How to get a cron job to start @reboot?

jasonwryan wrote:

The first line of the error message, cron runs in a limited environment and knows nothing about DISPLAY.

Use a systemd timer and a user service.

Also, use code tags when pasting to the boards: https://wiki.archlinux.org/index.php/Co … s_and_code

Okay so I have made a minimize.timer and minimize.service in the /etc/systemd/system path

minimize.timer

[Unit]
Description=Minimize on startup 

[Timer]
OnBootSec=10
Unit=minimize.service

[Install]
WantedBy=timers.target

minimize.service

[Unit]
Description=MinimizeAtBoot

[Service]
Type=oneshot
ExecStart=xdotool key super+d & DISPLAY=:0.0

is that all correct?

And if so, then I just have to systemctl start and enable it right?

Last edited by chris190 (2017-04-11 04:34:24)

Offline

#5 2017-04-11 04:58:32

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: How to get a cron job to start @reboot?

No. There is an env variable you use in the service file. Also, you want a --user not a system service. See the wiki page for details.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2017-04-11 06:29:38

chris190
Member
Registered: 2017-04-05
Posts: 3

Re: How to get a cron job to start @reboot?

jasonwryan wrote:

No. There is an env variable you use in the service file. Also, you want a --user not a system service. See the wiki page for details.

Okay after looking over the wiki/other tutorials for a while and i'm stumped here,

I have made a /usr/lib/systemd/minimize.service file

[Unit]
Description=MinimizeAtBoot

[Service]
Type=simple
ExecStart=/home/chris/test.sh

Then I made a /usr/lib/systemd/minimize.timer file

[Unit]
Description=Minimize on startup

[Timer]
OnBootSec=20
Unit=minimize.service

[Install]
WantedBy=timers.target

Then I ran systemctl --user enable minimize.timer


It works when I run systemctl --user start minimize.service, but the timer does not work on reboot.

I know im messing up somewhere, do I need to enable minimize.service too? or instead of simple run as oneshot? or some other command entirely?

edit: thanks for the help btw! its been really helpful so far smile

Last edited by chris190 (2017-04-11 06:30:22)

Offline

#7 2017-04-11 06:45:05

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: How to get a cron job to start @reboot?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB