# Help with a simple Cron job to understand the concept

**URL:** <https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106>\
**Category:** General\
**Created:** [24 October 2019 19:35 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106 "2019-10-24T19:35:44Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [25 October 2019 10:47 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/21 "2019-10-25T10:47:46Z")

</div>

> [@pvanklink](#):
>
> There a more then one switch time range  
> How do i know which is the correct?

Look at each one and work out which best suits your requirement. In addition look at the github page for each one (it is linked from the npm page) and look to see if there are outstanding issues and whether there have been any recent updates. No recent updates is not necessarily a problem as a simple node will just keep working once it is going.

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [25 October 2019 12:15 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/22 "2019-10-25T12:15:44Z")

</div>

OK, i tried different scenarios and now i have:

1. when there are files but the timeslot is closes, a switch in domoticz is turned on
2. when the timeslot is open and there are files, they are copied
3. when there are no more files, domoticz switch is turned of
4. when the timeslot is open for the first time files are copied in case there are no changes..

I am a newbie so it will look terrible..  
If someone can make this better, your welcome!  
Here my code for newbies like me who can use this!

Again, thanks for so far, my motion storage is solved!

```auto
[
    {
        "id": "eb2a7f4f.31f93",
        "type": "tab",
        "label": "Move to NAS",
        "disabled": false,
        "info": ""
    },
    {
        "id": "9d399608.e3a128",
        "type": "exec",
        "z": "eb2a7f4f.31f93",
        "command": "sudo mv /mnt/mntroot/* /mnt/mntnas",
        "addpay": false,
        "append": "",
        "useSpawn": "false",
        "timer": "5",
        "oldrc": false,
        "name": "copy motionoutput",
        "x": 451.5,
        "y": 99.5,
        "wires": [
            [
                "6a5caeae.60ac8",
                "ca9da11f.fbd25"
            ],
            [],
            []
        ]
    },
    {
        "id": "ea0a3041.6c8bc",
        "type": "comment",
        "z": "eb2a7f4f.31f93",
        "name": "How do i connect this exec to my cron job",
        "info": "",
        "x": 889.5,
        "y": 1008,
        "wires": []
    },
    {
        "id": "718066f6.336598",
        "type": "inject",
        "z": "eb2a7f4f.31f93",
        "name": "a time",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 112.5,
        "y": 99,
        "wires": [
            [
                "9d399608.e3a128"
            ]
        ]
    },
    {
        "id": "ca9da11f.fbd25",
        "type": "switch",
        "z": "eb2a7f4f.31f93",
        "name": "No more files?",
        "property": "rc.code",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 716.5,
        "y": 88,
        "wires": [
            [
                "6a5caeae.60ac8"
            ],
            [
                "7d97f9f0.e5d588",
                "6a5caeae.60ac8"
            ]
        ]
    },
    {
        "id": "b8e7e9a8.835c08",
        "type": "function",
        "z": "eb2a7f4f.31f93",
        "name": "files there",
        "func": "msg.payload = {};\nmsg.payload.command = \"switchlight\";\nmsg.payload.idx = 1749;\nmsg.payload.switchcmd = \"On\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 949.5,
        "y": 206,
        "wires": [
            [
                "e5bac5f2.5d4258"
            ]
        ]
    },
    {
        "id": "7d97f9f0.e5d588",
        "type": "function",
        "z": "eb2a7f4f.31f93",
        "name": "files not there",
        "func": "msg.payload = {};\nmsg.payload.command = \"switchlight\";\nmsg.payload.idx = 1749;\nmsg.payload.switchcmd = \"Off\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 970.5,
        "y": 93,
        "wires": [
            [
                "e5bac5f2.5d4258"
            ]
        ]
    },
    {
        "id": "e5bac5f2.5d4258",
        "type": "mqtt out",
        "z": "eb2a7f4f.31f93",
        "name": "",
        "topic": "domoticz/in",
        "qos": "",
        "retain": "",
        "broker": "465cbf12.c733c",
        "x": 1184.5,
        "y": 206,
        "wires": []
    },
    {
        "id": "2fcb1c01.6b8504",
        "type": "watch",
        "z": "eb2a7f4f.31f93",
        "name": "watch mntroot",
        "files": "/mnt/mntroot/",
        "recursive": "",
        "x": 114.5,
        "y": 454,
        "wires": [
            [
                "6a5caeae.60ac8",
                "de352d7f.0e3aa",
                "e9fdf674.c7fa08"
            ]
        ]
    },
    {
        "id": "195224b7.dfb92b",
        "type": "function",
        "z": "eb2a7f4f.31f93",
        "name": "c check time",
        "func": "let myHour = (new Date()).getHours();\nlet valid = (myHour >=6 && myHour <=24);\nif (valid)\n{msg.payload = true}\nelse \n{msg.payload = false}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 114.5,
        "y": 579,
        "wires": [
            [
                "f0b4bb9a.a490d8"
            ]
        ]
    },
    {
        "id": "f0b4bb9a.a490d8",
        "type": "switch",
        "z": "eb2a7f4f.31f93",
        "name": "check time is between 6 and 24",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 582.5,
        "y": 577,
        "wires": [
            [
                "9d399608.e3a128"
            ],
            []
        ]
    },
    {
        "id": "6a5caeae.60ac8",
        "type": "debug",
        "z": "eb2a7f4f.31f93",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 845.5,
        "y": 453,
        "wires": []
    },
    {
        "id": "eb6e2576.6bab08",
        "type": "comment",
        "z": "eb2a7f4f.31f93",
        "name": "option b",
        "info": "",
        "x": 257.5,
        "y": 266,
        "wires": []
    },
    {
        "id": "de352d7f.0e3aa",
        "type": "time-switch",
        "z": "eb2a7f4f.31f93",
        "name": "b check time",
        "lat": "52.022999",
        "lon": "4.312723",
        "startTime": "06:10",
        "endTime": "24:00",
        "startOffset": 0,
        "endOffset": 0,
        "x": 278.5,
        "y": 310,
        "wires": [
            [
                "9d399608.e3a128"
            ],
            [
                "6a5caeae.60ac8"
            ]
        ]
    },
    {
        "id": "f017efff.b4b5e",
        "type": "comment",
        "z": "eb2a7f4f.31f93",
        "name": "option a",
        "info": "",
        "x": 103.5,
        "y": 58,
        "wires": []
    },
    {
        "id": "3386dd47.6ff742",
        "type": "comment",
        "z": "eb2a7f4f.31f93",
        "name": "option c (first connect t watch instead of b)",
        "info": "",
        "x": 206.5,
        "y": 535,
        "wires": []
    },
    {
        "id": "e9fdf674.c7fa08",
        "type": "switch",
        "z": "eb2a7f4f.31f93",
        "name": "Are there files?",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "/",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 412.5,
        "y": 396,
        "wires": [
            [
                "b8e7e9a8.835c08"
            ]
        ]
    },
    {
        "id": "465cbf12.c733c",
        "type": "mqtt-broker",
        "z": "",
        "name": "mqttdomoticz",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

```

 ![Naamloos](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/de99e5c5f3ca606fac17275b20c0b04d74032d7a.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 October 2019 12:23 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/23 "2019-10-25T12:23:43Z")

</div>

Not sure which time range node that is, but i am wondering, do you need a timerange node ?  
You have a watch node, that could be your trigger right, but you should watch the nas folder instead.

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [25 October 2019 12:49 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/24 "2019-10-25T12:49:08Z")

</div>

my nas folder is at sleep at night, so thats why i write to a local folder and when the right time is there is sync.

Every looks to work ok, but when my motion is writing and the nas is awake is it can be synchronised i have a problem that motion is still writing and watch node syncs the file incomplete/corrupt?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 October 2019 13:11 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/25 "2019-10-25T13:11:44Z")

</div>

> my nas folder is at sleep at night

I understand that, but by watching the nas folder you know if it is available for copying.

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [25 October 2019 13:21 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/26 "2019-10-25T13:21:15Z")

</div>

mmm, but when it is available, there are no file changes on it, so will watch being triggered?

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [25 October 2019 13:35 UTC](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106/27 "2019-10-25T13:35:07Z")

</div>

I think i solved it for now...

I added an extra sleeptimer of 5 minutes  
and changed my move command in:  
sudo find /mnt/mntroot -maxdepth 1 -mmin +4 -type f -exec mv "{}" /mnt/mntnas/ ;

[Previous page](https://discourse.nodered.org/t/help-with-a-simple-cron-job-to-understand-the-concept/17106.md?page=1)
