Skip to contentSkip to conversation

package/dcron: create directories if needed on startup

PATCH New Christopher McCrory 2 participants 3 messages Jan 2, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 package/dcron: create directories if needed on startup

Create the /var/spool/cron/crontabs and /var/spool/cron/cronstamps directories on startup if they don't exist. By default /var/spool is a symlink to /tmp so the contents get deleted on reboot. If the cronstamps directory does not exist anything in /etc/cron.hourly/ , etc. will fail to run.

---
 package/dcron/S90dcron      | 105 +++++++++++++++++++++++++++++-------
 package/dcron/dcron.service |   6 +++
 2 files changed, 93 insertions(+), 18 deletions(-)
package/dcron/S90dcron +87 -18
index de21d2ca13..f51513d41e 100644
@@ -1,22 +1,91 @@
 #!/bin/sh
 
+# Make sure umask is sane
+umask 022
+
+PATH="/sbin:/usr/sbin:/bin:/usr/bin"
+export PATH
+
+DAEMON_NAME="dcron"
+DAEMON_DESC="dcron"
+DAEMON_BINARY="/usr/sbin/crond"
+DAEMON_BASENAME="crond"
+DAEMON_CONFIG="/etc/cron.d/system"
+DAEMON_ARGS="-- -f"
+DAEMON_PID="/var/run/crond.pid"
+
+[ -r /etc/default/$DAEMON_BASENAME ] && . /etc/default/$DAEMON_BASENAME
+[ -x $DAEMON_BINARY ] || exit 5
+[ -r $DAEMON_CONFIG ] || exit 6
+
+start() {
+  printf "Starting $DAEMON_DESC: "
+  [ -d /var/spool/cron/crontabs   ] || mkdir -vp /var/spool/cron/crontabs
+  [ -d /var/spool/cron/cronstamps ] || mkdir -vp /var/spool/cron/cronstamps
+  start-stop-daemon -S -q -b  -m -p $DAEMON_PID  -x $DAEMON_BINARY $DAEMON_ARGS
+  sleep 2 ; status ; RETVAL=$?
+  if [ $RETVAL -ne 0 ]; then
+    echo "FAIL"
+  else
+    echo "OK"
+  fi
+  return $RETVAL
+}
+
+# -R doesn't work with busybox, but doesn't error out
+stop() {
+  printf "Stopping $DAEMON_DESC: "
+  start-stop-daemon -K -R 59 -x $DAEMON_BINARY
+  RETVAL=$?
+  if [ $RETVAL -ne 0 ]; then
+    echo "FAIL"
+  else
+    echo "OK"
+  fi
+  return $RETVAL
+}
+
+status() {
+  PIDS=`pidof $DAEMON_BASENAME`
+  RETVAL=$?
+  if [ $RETVAL -eq 0 ]; then
+    echo "RUNNING (pid $PIDS)"
+  else
+    echo "NOT OK"
+  fi
+  return $RETVAL
+}
+
 case "$1" in
-	start)
-		printf "Starting cron ... "
-		start-stop-daemon -S -q -m -b -p /var/run/dcron.pid --exec /usr/sbin/crond -- -f
-		echo "done."
-		;;
-	stop)
-		printf "Stopping cron ..."
-		start-stop-daemon -K -q -p /var/run/dcron.pid
-		echo "done."
-		;;
-	restart)
-		$0 stop
-		sleep 1
-		$0 start
-		;;
-	*)
-		echo "usage: $0 {start|stop|restart}"
-		;;
+  start)
+    start
+    ;;
+  stop)
+    stop
+    ;;
+  restart)
+    echo "Restarting $DAEMON_DESC: "
+    stop
+    sleep 1
+    start
+    echo ""
+    ;;
+  try-restart)
+    status
+    if [ $RETVAL -eq 0 ]; then
+      stop
+      sleep 1
+      start
+    fi
+    ;;
+  status)
+    printf "Status of $DAEMON_DESC: "
+    status
+    ;;
+  *)
+    echo "Usage: $0 {start|stop|restart|status|try-restart}" >&2
+    exit 3
+    ;;
 esac
+
+exit 0
package/dcron/dcron.service +6 -0
index 924ed72205..19c66b1535 100644
@@ -3,6 +3,12 @@ Description=Task scheduler daemon
 After=syslog.target
 
 [Service]
+# make sure directories exist. use -v so it gets logged
+ExecStartPre=/bin/sh -c '\
+test -d /var/spool/cron/crontabs   || mkdir -pv /var/spool/cron/crontabs  ; \
+test -d /var/spool/cron/cronstamps || mkdir -pv /var/spool/cron/cronstamps; \
+'
+
 ExecStart=/usr/sbin/crond -S
 Type=forking
 
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/1 package/dcron: create directories if needed on startup

Hello Christopher,

Quoted 6 lines · On Thu, 2 Jan 2020 12:37:46 +0000 Christopher McCrory <[email protected]>…

Create the /var/spool/cron/crontabs and /var/spool/cron/cronstamps directories on startup if they don't exist. By default /var/spool is a symlink to /tmp so the contents get deleted on reboot. If the cronstamps directory does not exist anything in /etc/cron.hourly/ , etc. will fail to run.

We already have http://patchwork.ozlabs.org/patch/1196301/ pending to fix the same issue. Could you instead have a look at Carlos patch and see if it fixes the problem for you as well ?

Best regards,

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw

PATCH 1/1 package/dcron: create directories if needed on startup

Quoted 10 lines · Thomas Petazzoni

Hello Christopher,

Quoted 1 line · On Thu, 2 Jan 2020 12:37:46 +0000 Christopher McCrory <[email protected]>…

Create the /var/spool/cron/crontabs and /var/spool/cron/cronstamps

directories

Quoted 1 line

on startup if they don't exist. By default /var/spool is a symlink to

/tmp so

Quoted 1 line

the contents get deleted on reboot. If the cronstamps directory does

Quoted 10 lines

not exist

Quoted 3 lines

anything in /etc/cron.hourly/ , etc. will fail to run.

We already have http://patchwork.ozlabs.org/patch/1196301/ pending to fix the same issue. Could you instead have a look at Carlos patch and see if it fixes the problem for you as well ?

It looks like it would work. Its a superset of what I did. The same basic changes are made to the two init bits for creating the directories. The source code patch addresses the bigger picture. The directory creating bits from the earlier patch or my patch should fix the immediate bug. Changing the way dcrond starts up can be thought of as a different issue.

Best regards,

Quoted 7 lines

Thomas
--
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

Signature · 7 lines
Christopher McCrory
To the optimist, the glass is half full.
To the pessimist, the glass is half empty.
To the engineer, the glass is twice as big as it needs to be.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/buildroot/attachments/20200102/ae0423f2/attachment.html>