# Julia script using zmq works run from cli but NOT from cronjob

**URL:** <https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447>\
**Category:** General Usage\
**Tags:** zmq, linux, cron\
**Created:** [May 3, 2022, 9:00pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447 "2022-05-03T21:00:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [May 3, 2022, 9:00pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/1 "2022-05-03T21:00:58Z")

</div>

Hi there

not sure if this is a linux ( I’m running linux mint 20.3) and have set up 3 cronjobs.

the first starts a ZMQ PULL server that gets messages and println’s the messages to stdout which I pipe out to a text file thus

```julia
45 5 * * 1,2,3,4,5 julia ZMQ_PULL_SERVER_BASIC_5555.jl > "$(date +"\%Y_\%m_\%d_\%I_\%M_\%p")_daily_zmq.txt"

```

the PULL server will be terminated by an “END” message.

then I start a script that opens the ZMQ PULL port PUSHES messages to the PULL server and closes the port but does NOT send an “END” message so the ZMQ is still running.

I then start another julia script that opens the ZMQ PULL port and PUSHES messages to the PULL port and when finished sends “END” which closes the ZMQ PULL server down.

the issue is that the TXT file exists but has nothing in it when the scripts are run from the cronjobs

BUT  
IF I run the scripts by cut an pasting the cron entries the TXT file has been created and the data written correctly.

does julia handle the piping differently in the two situations?

thank you

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [May 3, 2022, 10:07pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/2 "2022-05-03T22:07:14Z")

</div>

> [@anon69491625](#):
>
> ` julia ZMQ_PULL_SERVER_BASIC_5555.jl`

imho:

- try to use full path - everywhere (juliafile, output ) !  
cronjob has a different user / workspace !

like  
`julia /my/project/ZMQ_PULL_SERVER_BASIC_5555.jl > /my/project/output.txt`

or write a wrapper bash job - to `cd` to the project directory  
`01 00 * * * cd /company/juliaproject003 && ./daily_zmq_job.sh`

EDIT:  
tips for debugging: [How to Test Cron Job?(Cron Job Testing) - DEV Community](https://dev.to/promode/how-to-test-cron-job-cron-job-testing-obk)  
`0/1 * * * * /usr/bin/julia /home/my_username/hello.jl > /home/my_username/log.txt`

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [May 3, 2022, 10:24pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/3 "2022-05-03T22:24:10Z")

</div>

thank you for the reply. I “think” I am using all the full paths. I trimmed the layout in the OP to simplify things.

Does this look right to you? It’s the FULL crontab layout that’s causing the issue.

```julia
45 5 * * 1,2,3,4,5 julia /home/dave/tontine_2022/test/4_15_22_ZMQ_PULL_SERVER_BASIC_5555.jl > /home/dave/tontine_2022/data/zmq/"$(date +"\%Y_\%m_\%d_\%I_\%M_\%p")_daily_zmq.txt"

00 6 * * 1,2,3,4,5 julia /home/dave/tontine_2022/test/5_3_22_live_STK_HV20_10_5_price+percent_CLOSE.jl 

# 00 7 * * 1,2,3,4,5 julia /home/dave/tontine_2022/test/4_12_22_live_STK_IV_percentile_close.jl

```

I’m pretty sure it’s something to do with ZMQ not liking the PULL server being opened by one script, written to, the script closing the port and another script opening it and writing to it, finally sending “END” to tell the PULL server to shut down. For some reason the PULL server is not closing the TXT file correctly.  
thank you for the debugging link very handy but I’d already worked through most of the steps before posting this question. The cronjobs run on time, the text file gets created, the scripts are NOT running ( I use htop to check ) at least they didn’t this morning when it ran.

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [May 3, 2022, 10:42pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/4 "2022-05-03T22:42:57Z")

</div>

> [@anon69491625](#):
>
> but I’d already worked through most of the steps before posting this question.

OK; So your code is running with `root` user ; from the `/` (root) dir …

```bash
sudo su
cd / 
julia /home/dave/tontine_2022/test/5_3_22_live_STK_HV20_10_5_price+percent_CLOSE.jl 

```

but from the crontab this code is not working … 🤔

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [May 3, 2022, 10:49pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/5 "2022-05-03T22:49:43Z")

</div>

Hi there  
sorry I don’t understand what you mean when you post. I didn’t mean to be rude by saying that the link to the cron debugging was not helpful, it was. I’m still working through it.

```julia
sudo su
cd / 
julia /home/dave/tontine_2022/test/5_3_22_live_STK_HV20_10_5_price+percent_CLOSE.jl

```

when I run from the CLI I’m logged in as user dave, if that helps. dave is an administrator account type  
Am I doing something wrong? Linux is NOT my strong suit. Thank you for bearing with me on this.

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [May 3, 2022, 11:09pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/6 "2022-05-03T23:09:29Z")

</div>

> [@anon69491625](#):
>
> when I run from the CLI I’m logged in as user dave, if that helps.

ok so you are running “cronjob” with the `dave` user

- and you can see your task list with : `crontab -u dave -l`

🤔

ideas:  
1.) maybe you have to “redirect standard error” to file - and check the output …  
like in this example ( `2>&1` in the end of the command line )

- [sh - How to redirect stderr to a file in a cron job - Stack Overflow](https://stackoverflow.com/a/34967179)

2.) You have to test the “environment” variables

`* * * * * env > /tmp/env.output`

any path missing ? ( [source](https://stackoverflow.com/a/54791559) )

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [May 3, 2022, 11:14pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/7 "2022-05-03T23:14:25Z")

</div>

ok…

check this post

> [@Running a specific julia environment from a crontab](https://discourse.julialang.org/t/running-a-specific-julia-environment-from-a-crontab/58799):
>
> Hi there. My goal is to run a Julia script from crontab. For this I need to be able to activate the correct Julia environment from the command line. I’m not sure how to do this. A possible solution I’ve found is to add code to the startup.jl such as: using Pkg if isfile("Project.toml") && isfile("Manifest.toml") Pkg.activate(".") end Is this an appropriate/best solution?

and add a project path :  
`julia --project=/path/to/your/project/folder /path/to/your/script.jl > /path/to/your/output.txt `

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [May 3, 2022, 11:20pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/8 "2022-05-03T23:20:11Z")

</div>

first of all thank you for doing something I should have! I clearly didn’t search for this on discourse and that’s very poor of me, sorry about that.

I’ll make the modifications as below

```julia
45 5 * * 1,2,3,4,5 julia --project=/home/dave/tontine_2022/test/4_15_22_ZMQ_PULL_SERVER_BASIC_5555.jl > /home/dave/tontine_2022/data/zmq/"$(date +"\%Y_\%m_\%d_\%I_\%M_\%p")_daily_zmq.txt"

00 6 * * 1,2,3,4,5 julia --project=/home/dave/tontine_2022/test/5_3_22_live_STK_HV20_10_5_price+percent_CLOSE.jl 

00 7 * * 1,2,3,4,5 julia --project=/home/dave/tontine_2022/test/4_12_22_live_STK_IV_percentile_close.jl

```

and see what happens. AGAIN so sorry for not searching before bothering you.

---

<div class="post-metadata">

**Author:** ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)\
**Post date:** [May 4, 2022, 1:51am UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/9 "2022-05-04T01:51:31Z")

</div>

> `julia --project=/home/dave/tontine_2022/test/4_12_22_live_STK_IV_percentile_close.jl`

imho:

- the `--project` command line option is expecting a `<dir>` parameter
  - [https://docs.julialang.org/en/v1/manual/command-line-options/](https://docs.julialang.org/en/v1/manual/command-line-options/)

based on my past experiences with crontab:

I am suggesting

- adding a `cd \<projdir>` before calling julia;
  - for me it is important if I use some relative path; inside my code!
  - so it is “safe” method …

- `&&` ( [chaning operator](https://stackoverflow.com/questions/4510640/what-is-the-purpose-of-in-a-shell-command) )
- julia command with 2 parameter !
  - `--project=/<projdir>`
  - `/<projdir>/juliaprog.jl` ( julia program with full path ! )

so your full command look like this:

- `00 7 * * 1,2,3,4,5 cd /home/dave/projdir && julia --project=/home/dave/projdir /home/dave/projdir/juliaprog.jl > /home/dave/projdir/output.txt `

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [May 4, 2022, 2:36am UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/10 "2022-05-04T02:36:16Z")

</div>

hi there  
WOW thanks again. I have taken your suggestion and made the modifications so the crontab now looks like this, as the scripts are in the /home/dave/tontine\_2022/test folder.

```julia
45 5 * * 1,2,3,4,5 cd /home/dave/tontine_2022/test && julia --project=/home/dave/tontine_2022/test /home/dave/tontine_2022/test/4_15_22_ZMQ_PULL_SERVER_BASIC_5555.jl > /home/dave/tontine_2022/data/zmq/"$(date +"\%Y_\%m_\%d_\%I_\%M_\%p")_daily_zmq.txt"

00 6 * * 1,2,3,4,5 cd /home/dave/tontine_2022/test && julia --project=/home/dave/tontine_2022/test /home/dave/tontine_2022/test/5_3_22_live_STK_HV20_10_5_price+percent_CLOSE.jl

00 7 * * 1,2,3,4,5 cd /home/dave/tontine_2022/test && julia --project=/home/dave/tontine_2022/test /home/dave/tontine_2022/test/4_12_22_live_STK_IV_percentile_close.jl

```

fingers crossed for tomorrow 🙂 thanks again for all your GREAT help.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [May 4, 2022, 1:02pm UTC](https://discourse.julialang.org/t/julia-script-using-zmq-works-run-from-cli-but-not-from-cronjob/80447/11 "2022-05-04T13:02:49Z")

</div>

hi there @ImreSamu so it didn’t work. The text file was created but nothing put into it. I’m going to move on from this and approach the goal from a different route. I thank you so much for all your efforts and they have NOT been wasted. I have learnt a lot from your replies and it’s given me a new view on cron in relation to julia. I can’t mark this as solved but I CAN mark the link with the specific julia environment definition in cron as the most probable solution. Thank you again for all your help
