I have an Ubuntu Server machine in which I need to run a Python script:

  • at boot and then every 30 minutes
  • with root permission
  • without logging in

I searched on the internet and I found out that I should use sudo crontab -e to edit my cron settings and add a line like this one:

 30 * * * * python3 /myScriptPath/myScriptName.py

What I couldn't find is how to tell cron that my script must be run at boot and then every 30 minutes and not every hour at "hh:30" time.

Also, where should I store my script?

  • I think this post might help: stackoverflow.com/questions/584770/…
    – user993560
    Nov 24, 2019 at 21:18
  • With systemd timers you can achieve this effect. It may not be possible with cron. See wiki.archlinux.org/index.php/Systemd/Timers
    – vanadium
    Nov 25, 2019 at 9:27
  • @Steve It's not what I'm looking for. I need to run my script immediately after the boot and then every thirty minutes. So, if I boot by system at 08:01 the script must be run at 08:01, 08:31, 09:01 and so on, not at 08:30, 09:00, 09:30 and so on.
    – Ntakwetet
    Nov 25, 2019 at 18:39

1 Answer 1


Write a control file and place it in /etc/cron.d/myscriptrun

@reboot   root    /usr/local/bin/myscript.sh

Do not make this file executable. It is a control file not a script.

Write a bash script and place it in /usr/local/bin/myscript.sh


while true ; do

    python3 /myScriptPath/myScriptName.py &
    sleep 30m


Make it executable chmod a+x /etc/cron.d/bashscript.

The & starts the job in the background so the script will sleep for exactly 30 minutes. You can remove the & and that changes the script to sleep 30 minutes after the job ends. Meaning jobs no longer start 30 minutes apart.

Cron runs your job

You don't need to start the script, cron does that automatically at boot time. To monitor status use:

$ systemctl status cron*
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-11-12 06:01:27 MST; 1 weeks 6 days ago
     Docs: man:cron(8)
 Main PID: 1115 (cron)
   CGroup: /system.slice/cron.service
           ├─1115 /usr/sbin/cron -f
           ├─1132 /usr/sbin/CRON -f
           ├─1138 /bin/sh -c    /usr/local/bin/eyesome.sh
           ├─1142 /bin/bash /usr/local/bin/eyesome.sh
           ├─1160 /bin/bash /usr/local/bin/eyesome-dbus.sh
           ├─1168 dbus-monitor --system type=method_call, interface=org.freedesktop.ColorManag
           ├─1169 /bin/bash /usr/local/bin/eyesome-dbus.sh
           └─6575 sleep 57207

Your display will have cron at the top and sleep at the bottom but will not have eyesome stuff in the middle, unless you are using that sunrise/sunset multiple monitor brightness/gamma transitioning software.

To see when your python job runs at it's next 30 minute interval run an inquiry on the process ID of the sleep command (which is 6575 in the example above):

$ remaining_sleep_time 6575

$ echo $((55923/60))

The time remaining is 55923 seconds divided by 60 seconds in a minute = 932 minutes before the job wakes up. To get a copy of remaining_sleep_time function see:

  • Should that run the script with root permission? It doesn't work for me, but if I run the script with sudo /myBashScript it does what it is supposed to do. Also, what is the & for?
    – Ntakwetet
    Nov 25, 2019 at 19:03
  • @Ntakwetet I've revised the answer to address your comments. Nov 25, 2019 at 23:50
  • I tried to use use your solution but my script is not run. $ systemctl status cron* doesn't show my script in the list, but there is an error in the log. There are two lines: the first is Error: bad minute; while reading /etc/cron.d/myscript, the second is (*system*myscript) ERROR (Syntac error, this crontab file will be ignored). Do you have any idea about what is happening? I have copied the script you posted as is, so I can't imagine what's the problem.
    – Ntakwetet
    Dec 6, 2019 at 20:58
  • Did you make it executable with chmod? Dec 6, 2019 at 22:26
  • I did. At first I forgot but I updated the permission when I got a permission error.
    – Ntakwetet
    Dec 8, 2019 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.