My program does not run correctly when I start it via crontab


New Member
Hi There,

This is linux programming question.
I have a server-type program, written in C, that listens to a socket, and provides several info to clients connected to it. When I run this program manually on a terminal, or on the background everything is fine. I can close the terminal and the program will keep serving clients as expected.
If, however, I start it from the crontab, some services don't run. For example a command like 'rc= system("ls > x.x") ; fails.

What can I do to fix this problem? Thanks for any tips


Well-Known Member
You could setup a cronjob that will do this for you so you won't have to manually run the commands in the terminal. Cron reads the crontab tables for commands and scripts.
By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.



Super Moderator
Staff member
Gold Supporter
G'day @gfyllos and welcome to :)

I am moving this Thread to Command Line, where such questions are best served, rather than being lost in General.

All will be notified.


Chris Turner


Well-Known Member
Without knowing anything much about your program - if it works normally when ran in one context and misbehaves when it's ran in another context - one possibility is it could be permissions related.

So for example - when it is attempting to perform an action, is it possible that the permissions of any files, or directories it uses are causing problems? e.g. preventing it from reading to/from a file/directory?

What level of privilege does the process run at when it fails? Is it running as root, or as a system user?

Also - have you tried using a debugger like gdb?
gdb can attach to a running process.

So you could try doing the following:
1. Build a debug version of your program, using the -g flag to generate debug information.
2. Launch the debug build of the program using whatever method is causing the programs execution to become problematic.
3. Launch gdb
4. Attach gdb to the PID of the running process using the following gdb command:
attach {PID}
Where {PID} is the numeric Process ID of the running executable.
5. Load the symbol table, by loading the actual executable itself:
symbol-file /path/to/debug-executable
Where /path/to/debug-executable is the path to the executable with the debug information.
6. Now set some breakpoints near the problematic areas of the program.
7. Make a client connection to your program (from another terminal, or another PC) and then wait to see where/if gdb bombs out anywhere.

Once GDB has broken out at a breakpoint - you can examine the state of the variables in your program and step through the problematic areas to see what's happening.

Otherwise, if that does not work (or you aren't au-fait enough with gdb) - the only other option would be to put some additional checks in your code. There may be some problems that you aren't checking for in your code? Or perhaps you'll need to put some logging functionality in there, so you can see what happens when your program is running.

gdb will probably be your best bet. It has a bit of a learning curve, if you are new to it. But it is worth investing some time in learning to use it.