omnilight/yii2-scheduling doesn't give any output - cron

I'm using the plugin omnilight/yii2-scheduling. When I'm just executing their commands using terminal in ubuntu, I get no output. Commands I'm executing :
php /somepath/vendor/yiisoft/yii2 yii schedule/list
php /somepath/vendor/yiisoft/yii2 yii schedule/run --scheduleFile=#app/config/schedule.php 1>> /dev/null 2>&1
Even I'm using their command for cronjob
* * * * * php /path/to/yii yii schedule/run --scheduleFile=#path/to/schedule.php 1>> /dev/null 2>&1
I'm not getting any output (Keep in mind that I'm not using any extension command to make it output the result into any file-by this it should give me output plainly then and there - right ?). Please advise where the problem lies.

Related

Redirection of stderr does not work when applications is excecuted as part of a cronjob

when calling
/home/username/temp/build/appname 2> /home/username/temp/log/stderr 1> /home/username/temp/log/stdout
both stderr is redirected to /home/username/temp/log/stderr, and stdout is redirected to /home/username/temp/log/stdout.
However, when adding a cronjob
5 * * * * username /home/username/temp/build/appname 2> /home/username/temp/log/stderr 1> /home/username/temp/log/stdout
The application runs as expected, stdout is redirected to /home/username/temp/log/stdout, but stderr is empty.
Any ideas?
EDIT:
The top of /etc/crontab is
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
The issue seems to be exclusive to my (commandline qt) application.
The following cronjob works with correct redirection of stderr:
* * * * * username ls /doesnotexist 2> /home/username/temp/log/ls_stderr
Depending on your distribution it might be that crontab is using a different shell than your are expecting. Two solutions come to mind:
Set the shell in crontab
Add SHELL=/bin/bash (or whichever shell you prefer) to the top of your crontab
Use the redirect syntax of the shell crontab is using
You can find the shell by adding
* * * * * echo $SHELL > $HOME/cronshell
to your crontab. It will most likely be /bin/sh, which again depending on your distribution might be symlinked to a different shell. Here you can find a great summary of different shell redirection syntaxes.

Trying to run script through crontab which won't work

I'm having issues getting my crontab to run I have the following line added to my crontab -e but it won't start. The command runs fine if I run it manually.
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py
Not getting any error messages and can't see the process when I run top or grep for it.
Usually this happens because the cron environment is different from your own. Make sure your start.py script uses full paths to any referenced files or external scripts. Make sure that your start.py script does not rely on environment variables that you have in your shell but it may not. Try piping the cron output to a mail command so you can see what it is doing, like so:
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py | mail -s "cron output" myself#example.com
An easier way to troubleshoot this is to write a wrapper shell script and send the output to a log file.
Create file python_start_cron.sh with contents
#!/bin/bash
cd /home/sam/p/ && /usr/bin/python3.5 start.py
Set the execute bit on this script script and make sure the script works manually
Modify the cronjob as shown below
0 */3 * * * python_start_cron.sh >/tmp/python_start_cron.log 2>&1
After cron executes, check the contents of the log file to ascertain the cause of the problem.

Error in crontab entry

I have been trying to make a cron entry for a shell script:
50 */4 * * * /path/script-file.sh > /dev/null 2>&1
aimed to run the script at HH:50 at a frequency of 4 hours. But this errors out with the message:
crontab: error on previous line; unexpected character found in line.
crontab: errors detected in input, no crontab file generated.
I removed the "/4" and the error vanished, but I know that cron does allow this format. Does anybody know what the issue could be?
Thank you very much for any help.
Some cron implementations don't support steps (e.g. */4) - check man 5 crontab on your particular system.
You can use the list 0,4,8,12,16,20 instead.
Off-topic: If you are using bash, you could probably replace > /dev/null 2>&1 with the shorter &>/dev/null or just close stdout and stderr with 1>&- 2>&-. (see #Keith Thompson's comment below)

Bash crontab doesn't output products of shell script file

Hi I'm a first year game programing student learning Unix Bash, I have run into a problem trying to understand crontab. I'm trying to do some rather simple things, checking to see if I am online, getting information about a given website, and ping another website to verify it is online. My script file does all of this without fail, however when I try to perform these tasks through crontab I get emails telling me absolutely nothing but jibberish. The output basically just tells me that I am trying to do all these things, but it doesn't output the results. I'm not sure where I am going wrong.
Just to verify I do have permission on the system to use crontab, and I have the script running every minute while I am trying to get it working. I'm hoping someone can point me in the right direction, all of my research online has really just led me astray.
This is my crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
LOGNAME=username
* * * * * /bin/bash /home/students/~/online.sh
30 23 1 * * rm online.log
this is my script
touch online.log
who | grep username >> online.log ; whois yahoo.ca >> online.log ; ping -c 1 www.google.com >> online.log
You need to use absolute paths in your scripts if you want to execute those using cron.
Note that cron executes in a different environment from what you get while executing a script on the command line. For example, changes lines like
touch online.log
to include the absolute path to online.log.
The output is being redirected into online.log, so you need to look there, not in your emails. If you want the output to be in the emails as well, you should look into using tee instead of a redirection.

Linux system command not working in Perl script via crontab

I have added a script in crontab for every 30 minutes. The line goes as follows:
*/30 * * * * root perl /root/perl.pl
The above script has a execution of system command 'top' and it gets printed in a log file.
If I run it manually it runs fine. But while running it via crontab, it does not show up the desired results. Please can somebody help me with this. Thank you.
The command in the above perl script is:
$top = `sudo top`;
The error I am getting is:
sudo: sorry, you must have a tty to run sudo
I changed my command from sudo to visudo. But still the problem remains.
You should use the full path in cron (and use which perl to find the full path):
*/30 * * * * root /usr/bin/perl /root/perl.pl
OR better yet, make the script executable using chmod +x, and add the interpreter to the beginning of the script #!/usr/bin/perl -w and call it directly from cron
*/30 * * * * root /root/perl.pl
Also if there is a problem in the perl script, you could output the result from cron like this
*/30 * * * * root /root/perl.pl > /tmp/myscript.log
You should look into the requiretty setting with regard to visudo. Look for a line that reads Defaults requiretty. You could try commenting it out, but you will be sacrificing some security. See man sudoers.
You could also try running top in batch mode with one iteration:
$top = `sudo top -bn1`;
Batch mode option is for sending output to other programs.

Resources