Crontab throwing error - linux

I have the following content in crontab:
20 1,7,13,18 * * * /usr/sbin/automysqlbackup
15 * * * * root find /opt/activeMq/activemq-data/localhost/KahaDB/ -mtime +10 -type f -delete
but I get this when I restart cron service:
Feb 20 08:43:27 . crontab[14584]: (root) BEGIN EDIT (root)
Feb 20 08:44:08 . crontab[14584]: (root) REPLACE (root)
Feb 20 08:44:08 . crontab[14584]: (root) END EDIT (root)
Feb 20 08:44:28 . kernel: [325740.483115] init: cron main process (14563) killed by TERM signal
Feb 20 08:44:28 . cron[14641]: (CRON) INFO (pidfile fd = 3)
Feb 20 08:44:28 . cron[14642]: (CRON) STARTUP (fork ok)
Feb 20 08:44:28 . cron[14642]: Error: bad username; while reading /etc/crontab
Feb 20 08:44:28 . cron[14642]: (*system*) ERROR (Syntax error, this crontab file will be ignored)
Feb 20 08:44:28 . cron[14642]: (CRON) INFO (Skipping #reboot jobs -- not system startup)

I found the solution. Username field is only available at system level cron(etc/crontab) and not user level cron.

Related

How to I properly prevent systemd suspend using a script in /lib/systemd/system-sleep/

I'm fairly new to Linux and trying to learn. I'm using Plex Media Server and I'm trying to prevent the system from sleeping while streaming a file. I've searched the internet over the last few days and none of the solutions seem to work. One solution I feel is almost getting me there, but it's not quite working. Here is the script I've placed (and made executable) in /lib/systemd/system-sleep/ (based on this site).
#!/bin/bash
PATH=/sbin:/usr/sbin:/bin:/usr/bin
X_DISPLAY_USERNAME=myusername
plexresume()
{
number_of_sessions=$(curl -s localhost:32400/status/sessions? | sed -n "s/.*MediaContainer size=\"\(.*\)\".*/\1/p")
if [ ${number_of_sessions} -gt 0 ]; then
echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session active, cancel suspend" >> /tmp/plex_sleep_log
return 1
else
echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session -IN-active, going to sleep now zzzzzzzz........" >> /tmp/plex_sleep_log
return 0
fi
}
plexkeepalive()
{
echo "[$(date +"%Y.%m.%d-%T")] Resuming!!..." >> /tmp/plex_sleep_log
su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool getmouselocation | grep 'x:1 y:1 '" > /dev/null
if [ "$?" == "0" ]; then
su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 9 9"
else
su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 1 1"
fi
return 0
}
case "$1" in
pre)
plexresume
;;
post)
plexkeepalive
;;
esac
I know it's executing because it's printing to the log file. But even when it's printing that Plex is active, it's still suspending the system. I've manually run the script using sudo outside of systemd and checking the value of $? after, which is 1.
When I use
journalctl -b -u systemd-suspend.service
I see the following:
Sep 26 12:47:03 systemname systemd[1]: Starting System Suspend...
Sep 26 12:47:03 systemname [141890]: /usr/lib/systemd/system-sleep/plexkeepalive failed with exit status 1.
Sep 26 12:47:03 systemname systemd-sleep[141887]: Entering sleep state 'suspend'...
One time I got a successful result, but I'm not sure how it happened:
Sep 26 10:51:45 systemname systemd-sleep[112491]: Entering sleep state 'suspend'...
Sep 26 10:52:25 systemname systemd-sleep[112491]: Failed to put system to sleep. System resumed again: Device or resource busy
Sep 26 10:52:25 systemname su[112556]: (to myusername) root on none
Sep 26 10:52:25 systemname su[112556]: pam_unix(su:session): session opened for user myusername(uid=1000) by (uid=0)
Sep 26 10:52:25 systemname su[112556]: pam_unix(su:session): session closed for user myusername
Sep 26 10:52:25 systemname su[112592]: (to myusername) root on none
Sep 26 10:52:25 systemname su[112592]: pam_unix(su:session): session opened for user myusername(uid=1000) by (uid=0)
Sep 26 10:52:25 systemname su[112592]: pam_unix(su:session): session closed for user myusername
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Main process exited, code=exited, status=1/FAILURE
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Failed with result 'exit-code'.
Sep 26 10:52:25 systemname systemd[1]: Failed to start System Suspend.
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Consumed 2.732s CPU time.
Any help on this issue would be appreciated. I don't understand why returning 1 from the script is not preventing systemd-suspend.service from running. Thank you!

Cron script executed but no output

I've taken a look to many different topics and did not find an answer to my problem. I created 2 bash scripts on my Ubuntu server and I'm trying to execute them periodically. It seems they are running, but they produce nothing. They are executable:
drwxr-xr-x 14 root root 4096 Mar 14 18:02 ..
-rwxr-xr-x 1 root root 2623 Apr 16 21:18 backup.pl
-rw-r--r-- 1 root root 87066352 May 10 21:37 full_site_backup-10-4-2018.tar.gz
-rwxr-xr-x 1 root root 530 May 11 20:21 checkHealth.sh
drwxr-xr-x 2 root root 4096 May 11 20:35 .
So here is one of my scripts:
#!/bin/bash
# log stdout and stderr to two different files
exec >>/var/log/test.log 2>>/var/log/test.err.log
# ...and log every command we try to execute to stderr (aka looog.err.log)
# set -x
CODE=$(curl -s -o /dev/null -I -A "myuseragent" -w "%{http_code}" https://www.xxxxxxxxxxxxx.xxx/xxxx)
DATE=$(date)
if [ $CODE -gt 300 ]
then
service mysql restart
service tomcat8 restart
>&2 echo "$DATE - KO !!!!!! code retour $CODE"
else
echo "$DATE - OK, code $CODE"
fi
and here is my sudo crontab -e :
# m h dom mon dow command
0 2 * * * root /usr/bin/perl /var/backup/backup.pl
* * * * * root /bin/sh /var/backup/checkHealth.sh
and here is my sudo tail -f /var/log/cron.log:
May 11 20:39:01 ns381471 CRON[10778]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
May 11 20:39:26 ns381471 crontab[10823]: (root) BEGIN EDIT (root)
May 11 20:40:01 ns381471 CRON[10880]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
May 11 20:40:01 ns381471 CRON[10879]: (root) CMD (/usr/local/rtm/bin/rtm 2 > /dev/null 2> /dev/null)
May 11 20:40:30 ns381471 crontab[10823]: (root) END EDIT (root)
May 11 20:41:01 ns381471 CRON[10974]: (root) CMD (/usr/local/rtm/bin/rtm 2 > /dev/null 2> /dev/null)
May 11 20:41:01 ns381471 CRON[10975]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
May 11 20:42:01 ns381471 CRON[11070]: (root) CMD (/usr/local/rtm/bin/rtm 2 > /dev/null 2> /dev/null)
May 11 20:42:01 ns381471 CRON[11071]: (root) CMD (root /bin/sh /var/backup/checkHealth.sh)
Any help would be appreciated. Thanks
Ok for those who are in the same situation, my crontab was
# m h dom mon dow command
0 2 * * * root /usr/bin/perl /var/backup/backup.pl
* * * * * root /bin/sh /var/backup/checkHealth.sh
It appears that I just copied the syntax you can find on many cron tutorials about editing /etc/crontab, but in my case I did a sudo crontab -e, so I did not have to specifiy the user (root), so the working entries were :
# m h dom mon dow command
0 2 * * * /usr/bin/perl /var/backup/backup.pl
* * * * * /bin/sh /var/backup/checkHealth.sh
or simplier :
# m h dom mon dow command
0 2 * * * /var/backup/backup.pl
* * * * * /var/backup/checkHealth.sh

Unable to run cron job with standard user account

The same job can be executed as root, but it can't execute as a standard user.
Is it permission problem or I need to change anything, I have no idea on it.
Thanks
SunOS 5.10 Generic_150400-30 sun4v sparc SUNW,SPARC-Enterprise-T5120
Command:
1) login as a root
2) crontab -l
* * * * * /usr/bin/date > /tmp/root.log
3) /tmp/root.log is here
1) login as a Non-root user
2) crontab -l
* * * * * /usr/bin/date > /tmp/non-root.log
3) /tmp/non-root.log is not here
The following permissions are OK for the binary file date
-bash-3.2# ls -l /usr/bin/date
-r-xr-xr-x 1 root bin 11056 Jan 22 2005 /usr/bin/date
-bash-3.2#
If the permissions are OK check your cron log on /var/cron/log file
-bash-3.2# tail /var/cron/log
< root 24592 c Fri Oct 20 18:50:21 2017
> CMD: /usr/bin/date > /tmp/non-root.log
> user 25192 c Fri Oct 20 18:51:00 2017
< user 25192 c Fri Oct 20 18:51:00 2017
> CMD: /scripts/collectdata.sh > /dev/null 2>&1
> root 25769 c Fri Oct 20 18:52:00 2017
< root 25769 c Fri Oct 20 18:52:00 2017
> CMD: /scripts/collectdata.sh > /dev/null 2>&1
> root 26853 c Fri Oct 20 18:54:00 2017
< root 26853 c Fri Oct 20 18:54:00 2017
-bash-3.2#
Thanks all, I finally found out the issue.
The reason is that non-root account is locked out, I think it maybe someone did many failure attempt which make this locked.
After I passwd -u "Account", the job can be run as expected. Thanks~

Cronjob is working, but my Script wont load in cron

My cron is working(Cron.log below) and my script is working too. But Cron wont load the script every 2 Minutes.
Cron.log:
Apr 3 01:02:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[11777]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:04:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[1754]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:06:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[15086]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:06:49 Debian-78-wheezy-64-minimal crontab[5657]: (root) BEGIN EDIT (root)
Apr 3 01:06:54 Debian-78-wheezy-64-minimal crontab[5657]: (root) END EDIT (root)
Apr 3 01:06:59 Debian-78-wheezy-64-minimal crontab[19549]: (mc) BEGIN EDIT (mc)
Apr 3 01:08:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[9566]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:08:21 Debian-78-wheezy-64-minimal crontab[19549]: (mc) END EDIT (mc)
Apr 3 01:08:26 Debian-78-wheezy-64-minimal crontab[12341]: (mc) BEGIN EDIT (mc)
Apr 3 01:09:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[7312]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5 $(/usr/lib/php5/maxlifetime))
Apr 3 01:10:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[29224]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:10:45 Debian-78-wheezy-64-minimal crontab[12341]: (mc) END EDIT (mc)
Apr 3 01:12:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[4285]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:14:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[12549]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:16:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[2767]: (mc) CMD (mc /home/mctw2/crash-script)
Apr 3 01:17:01 Debian-78-wheezy-64-minimal /USR/SBIN/CRON[18323]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
and here is my Script:
#!/bin/bash
SERVER=`ps ax | grep FTBServer-1.6.4-965.jar | grep -v grep | wc -l`
if [ $SERVER -eq 1 ]
then
exit
else
cd /home/mctw2/ ; screen -a -m -d -s mctw2 ./ServerStart.sh
fi
and my crontab at least:
*/2 * * * * mc /home/mctw2/crash-script
How can I make it work?
Your crontab entry
*/2 * * * * mc /home/mctw2/crash-script
contains the username mc in the 6th column as appropriate for system-level crontabs as in /etc/crontab.
However the format for per-user crontabs as edited using crontab -e is the following:
*/2 * * * * /home/mctw2/crash-script

gammu-smsd RunOnReceive script results exit status 2

I want to forward an SMS using gammu-smsd RunOnReceive.
That is the script I want to run (/var/spool/gammu/forward.sh) and it goes perfectly if I run it from a sudoer or using sudo -u gammu -g gammu /var/spool/gammu/forward.sh
#!/bin/bash
SMS_MESSAGES=1
for i in `seq $SMS_MESSAGES`
do
number="SMS_${i}_NUMBER"
text="SMS_${i}_TEXT"
eval "gammu-smsd-inject TEXT my_number_goes_here -text \"${!number}: ${!text}\""
done
And here is the problem I am experiencing:
Thu 2015/01/29 23:08:57 gammu-smsd[2549]: Starting run on receive: /var/spool/gammu/forward.sh IN20150130_000850_00_+37368214400_00.txt
Thu 2015/01/29 23:08:57 gammu-smsd[2154]: Process failed with exit status 2
Output of ls -l /etc/gammu-smsdrc /var/spool/gammu/ /usr/bin/gammu-smsd*:
-rw-r--r-- 1 root root 457 Jan 29 22:44 /etc/gammu-smsdrc
-rwxrwxrwx 1 root root 14336 Jun 10 2012 /usr/bin/gammu-smsd
-rwxrwxrwx 1 root root 51164 Jun 10 2012 /usr/bin/gammu-smsd-inject
-rwxrwxrwx 1 root root 9972 Jun 10 2012 /usr/bin/gammu-smsd-monitor
/var/spool/gammu/:
total 24
drwxrwxrwx 2 gammu gammu 4096 Jan 28 16:02 error
-rwxrwxrwx 1 gammu gammu 189 Jan 29 22:13 forward.sh
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:08 inbox
-rw-rw-r-- 1 gammu gammu 3702 Jan 29 23:08 log
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:07 outbox
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:07 sent
What happens if I just do ./forward.sh (not root) - so all is OK:
gammu-smsd-inject[2606]: Created outbox message OUTC20150029_231213_00_my_number_here_sms0.txt
Written message with ID /var/spool/gammu/outbox/OUTC20150029_231213_00_my_number_here_sms0.txt
Here is my /etc/gammu-smsdrc
# Configuration file for Gammu SMS Daemon
[gammu]
port = /dev/ttyUSB0
connection = at
[smsd]
service = files
logfile = /var/spool/gammu/log
debuglevel = 2
commtimeout = 1
sendtimeout = 15
statusfrequency = 0
outboxformat = unicode
transmitformat = unicode
RunOnReceive = /var/spool/gammu/forward.sh
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/
ps -fe | grep gammu:
gammu 2154 1 0 23:05 ? 00:00:02 /usr/bin/gammu-smsd --daemon --user gammu --pid /var/run/gammu-smsd.pid
cubie 2644 2403 0 23:20 pts/0 00:00:00 grep gammu
Please, help
I had the same problem and I solved it this way:
First add gammu user to sudoers, with no password:
type: $ sudo visudo
and add: gammu ALL=(ALL) NOPASSWD: ALL
Then run gammu-smsd as root user:
in /etc/init.d/gammu-smsd
change USER=gammu to USER=root
save it and don't forget to restart daemon: service gammu-smsd restart
In RunOnReceive script add sudo in front of gammu-smsd-inject:
e.g.: sudo gammu-smsd-inject TEXT my_tel_num -text "Hello world!"
I hope this will work for you too!
P.S.: I use Gammu version 1.31.90.

Resources