How to execute a go binary file with crontab?

0

I have created a binary file from a Go script, and now I need to run it with crontab. The binary file runs without issues executing it manually from the command line. I am using Ubuntu from WSL.
Crontab saves successfully, but it seems the script does not execute.

Cronjob looks like this:

*/1 01 * * * /home/tgr/test-bin/main/main -download > /dev/null 2>&1
  • I was testing this at 1 AM, that is why the crontab hour field is in 01.

Running the script with “-download” flag, tries to download some files from a bucket, using a YAML config file, then makes some secondary tasks like decompression, copying and verifying the structure of the files. It produces a log file too.

I have already started cron service with

sudo service cron start

Could you please advise?

I would first test if crontab is working by using a simpler command. It could be a bash script that outputs hello world and drop all these redirections.

Use also a simpler periodicity specification. The */1 means to run the command every minute. Is this what you want ? I’m not sure if the 01 is valid. I would replace it with 1 just to be sure.

Hello Christophe, thanks a lot for your answer.
Yes, I wanted the script to execute every minute, just to see the changes made in the log files quickly.
I have tested crontab with another simple go binary file, that only prints “Hello” in the STDOUT, redirecting it to a text file, and it worked as expected.

Good. Then it is very likely a problem in your binary. Try with removing the redirection to /dev/null so that any output prints to stdout and is sent to postmaster by mail. For testing, there is no need to run it every minutes because you will be drawn in mails if there is output.

By the way, you write that you made a go binary from a go script. There is normally no such thing as go scripts. There is only go code that must be compiled to become a binary that can be run.