I'm not sure why but my app never executes when I reboot my machine. In crontab

> crontab -e

I see

@reboot /root/myscriptname.sh

The file is

#!/bin/bash
nohup mono-sgen /root/myapp.exe /path/file > /dev/null 2>&1 &

I run

ps aux | grep mono

and I don't see my file. If i run /root/myscriptname.sh from the command line it runs fine. I tried using bash /root/myscriptname.sh in crontab but that didn't solve it. How do I execute mono-sgen /root/myapp.exe /path/file > /dev/null 2>&1 without putting it in crontab (I want to leave it in a easy to disable sh file)

First, you should check crontab log to make sure crontab runs normally.

Is mono-gen in your $PATH? You can check with this command: type mono-gen.

Try adding full path to mono-sgen and check result:

#!/bin/bash
nohup /path/to/mono-sgen /root/myapp.exe /path/file > /dev/null 2>&1 &

In general, when you have such issues, there are a few things you should always try:

  1. Redirect the error output of your program:

    @reboot /root/myscriptname.sh 2> /root/logfile.txt
    
  2. Increase the verbosity level of cron, add this line to /etc/default/cron (at least on Debian based systems) and restart the cron daemon:

    EXTRA_OPTS='-L 4'
    

    The available log options are:

    0   no logging (errors are logged regardless)
    1   log start of jobs
    2   log end of jobs
    4   log jobs with exit status != 0
    8   log the process identifier of child process (in all logs)
    

    You should find the logs in /var/syslog.

In your particular case, I am fairly sure the issue is that mono-sgen is not in cron's path as suggested by Gnouc but these are useful tricks to know for next time.

  • The first one should be taken care of since cron emails me any output. I actually am running ANOTHER mono app in cron which ... hey it isnt working. It stopped working ever since I upgraded debian from 6 to 7 (squeeze to wheezy) hmmm – user4069 Mar 4 '14 at 7:26
  • I had an issue logging the output from a script containing nohup, it was an script that I execute sometimes from command line, so I wished to use nohup besides it executes from crontab, if it helps someone the configuration that worked was: Inside crontab @reboot /root/myscriptname.sh and inside myscriptname.sh nohup command > /path/to/file.log 2&1 – Sotsir Nov 9 '15 at 18:14

The @reboot can be problematic depending on what distro you're using. See this U&L Q&A I wrote up about this very issue, titled: crontab's @reboot only works for root?.

Rather then mess with a crontab entry I'd be inclined to put a script together that can be run as part of you logging into your desktop environment (DE). This can then be added as "Startup Programs" via gnome-session-properties.

       ss #1

For modifications to your script see @Gnouc's answer.

Isn't it cleaner to put this in rc.local (or equivalent)? It's the standard way of doing things after boot. It's better, because it's not called when cron wakes up (which is in the middle of boot procedure) but after all the services are running. It's also more robust. If you want logging you can still use "logger" command.

Also, the script may have started, but failed after a moment and is thus not seen in ps.

  • I only need it to run for 3-5 days and I haven't made a init script for a while so i don't remember. – user4069 Mar 4 '14 at 15:02
  • Sorry I forgot debian does not usually use the rc.local script that just planly executes without initscript semantics. Still, I think this is a preferable way of doing things, much less tricky than one-shot cron job that may run as a different user. Still, rc.local is supposed to be handled by debian for compatibility reasons. Try it, for 5 days it does not matter if it works. But don't forget to background the process with & at the end (and possibly nohup at the beginning). If it does not terminate immediately, boot will wait for it. – orion Mar 4 '14 at 17:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.