Score:2

Cron Job created by script to run another script on reboot - not working

br flag

I'm trying to have a script create a cron job that executes another script on reboot. For reasons I am yet to understand, I cannot get this to work.

For the sake of simplicity, this is the snippet I cannot get working.

Script that creates cron job:

#!/bin/bash

crontab /home/user/folder/tmp/crontab-reboot-part-2.txt

Contents of /crontab-reboot-part-2:

@reboot /home/user/folder/script.sh >> /home/user/folder/cron.log 2>&1

Even when manually executing the crontab command before the reboot,

crontab /home/user/folder/tmp/crontab-reboot-part-2.txt

on reboot, the job still doesn't run as cron.log hasn't been created.

Any idea?

Thanks.

Edit: Per @Raffa's comment

ls -l /home/user/folder/script.sh outputs:

-rwxrwxr-x 1 user user 585 Aug 8 06:51 20_run_commissioning.sh

journalctl --grep='cron' --no-pager --since='-1hour' outputs:

-- Logs begin at Thu 2023-08-10 05:28:46 UTC, end at Thu 2023-08-10 13:31:09 UTC. --
Aug 10 13:31:04 ubuntu cron[706]: (CRON) INFO (pidfile fd = 3)
Aug 10 13:31:04 uncommissioned cron[706]: (CRON) INFO (Running @reboot jobs)
Aug 10 05:39:09 uncommissioned systemd[1]: cron.service: Succeeded.
-- Reboot --
Aug 10 05:40:06 uncommissioned cron[728]: (CRON) INFO (pidfile fd = 3)
Aug 10 05:40:06 uncommissioned cron[728]: (CRON) INFO (Running @reboot jobs)
Aug 10 05:40:06 uncommissioned CRON[732]: pam_unix(cron:session): session opened for user user by (uid=0)
Aug 10 05:40:06 uncommissioned CRON[766]: (user) CMD (/home/user/commissioning/20_run_commissioning.sh >> /home/user/commissioning/cron.log 2>&1^M)
Aug 10 05:40:06 uncommissioned CRON[732]: (CRON) info (No MTA installed, discarding output)
Aug 10 05:40:06 uncommissioned CRON[732]: pam_unix(cron:session): session closed for user user
Raffa avatar
jp flag
What is the output of `ls -l /home/user/folder/script.sh` and what does it contain ... Also, please reboot your system with that cronjob in the crontab and, after reboot, add the output of `journalctl --grep='cron' --no-pager --since='-1hour'` ... Also, I'm not sure if `@reboot` will run in a normal user's crontab as opposed to root's crontab.
Edward avatar
br flag
@Raffa see edit.
Score:1
br flag

I found the issue. It was the "^M" appended to the end of

Aug 10 05:40:06 uncommissioned CRON[766]: (user) CMD (/home/user/commissioning/20_run_commissioning.sh >> /home/user/commissioning/cron.log 2>&1^M)

from writing this file on my windows machine.

Thanks for the help @Raffa

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.