Jetson Nano unable to capture images through crontab

Hi!
I’m trying to capture image from a raspberry pi camera (CSI) and upload the image to my google drive automatically by using crontab.
My code works fine when I run it in terminal, and also work fine with just uploading an existing file to google drive by using crontab. However, it only failed when I tried to capture image using crontab or systemd.

I’ve been trying many methods, but it still not work at all.

Here is my python file:

import cv2
import time
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    gauth.Refresh()
else:
    gauth.Authorize()
gauth.SaveCredentialsFile("mycreds.txt")

def gstreamer_pipeline(
    capture_width=1280,
    capture_height=720,
    display_width=1280,
    display_height=720,
    framerate=60,
    flip_method=0,
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )


def show_camera(new_path):
    print(gstreamer_pipeline(flip_method=0))
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    if cap.isOpened():
        window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
        # Window
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            ret_val, img = cap.read()
            cv2.imwrite(new_path, img)
            
            break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print("Unable to open camera")


if __name__ == "__main__":
    now_str = time.localtime()
    new_path = str(now_str.tm_year) + str(now_str.tm_mon) + str(now_str.tm_mday) + str(now_str.tm_hour) + str(now_str.tm_min) + '.png'
    show_camera(new_path)
    upload_drive(new_path)

And this is my code in crontab -e:

* * * * * cd desktop/image_temp && /usr/bin/python3.6 test_cam_path.py >> test_cam_path.out

And this is the content in test_cam.out:

GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0 
   Camera mode  = 4 
   Output Stream W = 1280 H = 720 
   seconds to Run    = 0 
   Frame Rate = 120.000005 
GST_ARGUS: PowerService: requested_clock_Hz=12096000
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
CONSUMER: Done Success
WARNING Argus: 5 client objects still exist during shutdown:
	547367618656 (0x7f78002760)
	547372923680 (0x7f780016b0)
	547372927056 (0x7f780028c0)
	547372951104 (0x7f780043f0)
	547372959144 (0x7f78004508)

Also I’ve check the syslog, here is part of the messege:

Jul 16 18:24:01 user-desktop CRON[8791]: (user) CMD (cd desktop/image_temp && /usr/bin/python3.6 test_cam_path.py >> test_cam_path.out)
Jul 16 18:24:02 user-desktop nvargus-daemon[6684]: === NVIDIA Libargus Camera Service (0.97.3)=== Listening for connections...=== python3.6[8792]: Connection established (7F81CD61D0)OFParserListModules: module list: /proc/device-tree/tegra-camera-platform/modules/module0
Jul 16 18:24:02 user-desktop nvargus-daemon[6684]: OFParserListModules: module list: /proc/device-tree/tegra-camera-platform/modules/module1
Jul 16 18:24:02 user-desktop nvargus-daemon[6684]: OFParserGetVirtualDevice: NVIDIA Camera virtual enumerator not found in proc device-tree
Jul 16 18:24:02 user-desktop nvargus-daemon[6684]: ---- imager: No override file found. ----
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: LSC: LSC surface is not based on full res!
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: ---- imager: No override file found. ----
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: (NvCamV4l2) Error ModuleNotPresent: V4L2Device not available (in /dvs/git/dirty/git-master_linux/camera/utils/nvcamv4l2/v4l2_device.cpp, function findDevice(), line 256)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: (NvCamV4l2) Error ModuleNotPresent:  (propagating from /dvs/git/dirty/git-master_linux/camera/utils/nvcamv4l2/v4l2_device.cpp, function initialize(), line 60)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: (NvOdmDevice) Error ModuleNotPresent:  (propagating from dvs/git/dirty/git-master_linux/camera-partner/imager/src/devices/V4L2SensorViCsi.cpp, function initialize(), line 106)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: NvPclDriverInitializeData: Unable to initialize driver v4l2_sensor
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: NvPclInitializeDrivers: error: Failed to init camera sub module v4l2_sensor
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: NvPclStartPlatformDrivers: Failed to start module drivers
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: NvPclStateControllerOpen: Failed ImagerGUID 0. (error 0xA000E)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: NvPclOpen: PCL Open Failed. Error: 0xf
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter: Sensor could not be opened. (in src/services/capture/CaptureServiceDeviceSensor.cpp, function getSourceFromGuid(), line 582)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter:  (propagating from src/services/capture/CaptureService.cpp, function addSourceByGuid(), line 437)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter:  (propagating from src/api/CameraDriver.cpp, function addSourceByIndex(), line 295)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter:  (propagating from src/api/CameraDriver.cpp, function getSource(), line 458)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: Acquiring SCF Camera device source via index 1 has failed. === python3.6[8792]: CameraProvider initialized (0x7f7c844740)LSC: LSC surface is not based on full res!
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: === python3.6[8792]: CameraProvider destroyed (0x7f7c844740)=== python3.6[8792]: WARNING: Cleaning up 1 outstanding requests...=== python3.6[8792]: WARNING: Cleaning up 1 outstanding streams...SCF: Error InvalidState: 4 buffers still pending during EGLStreamProducer destruction (propagating from src/services/gl/EGLStreamProducer.cpp, function freeBuffers(), line 305)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error InvalidState:  (propagating from src/services/gl/EGLStreamProducer.cpp, function ~EGLStreamProducer(), line 50)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: === python3.6[8792]: WARNING: Cleaning up 1 outstanding stream settings...=== python3.6[8792]: WARNING: Cleaning up 1 outstanding sessions...(NvCameraUtils) Error InvalidState: Mutex not initialized (/dvs/git/dirty/git-master_linux/camera/core_scf/src/services/gl/EGLStreamProducer.cpp:497) (in Mutex.cpp, function lock(), line 79)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter: Buffer is not pending (in src/services/gl/EGLStreamProducer.cpp, function presentBufferInternal(), line 501)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: (NvCameraUtils) Error InvalidState: Mutex has not been initialized (in Mutex.cpp, function unlock(), line 88)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter:  (propagating from src/services/gl/EGLStreamProducer.cpp, function presentBuffer(), line 486)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter:  (propagating from src/components/CaptureContainerImpl.cpp, function returnBuffer(), line 447)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter:  (propagating from src/components/stages/BufferReturnStage.h, function doExecute(), line 43)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: SCF: Error BadParameter: Sending critical error event (in src/api/Session.cpp, function sendErrorEvent(), line 990)
Jul 16 18:24:03 user-desktop nvargus-daemon[6684]: (NvCameraUtils) Error InvalidState: Mutex not initialized (/dvs/git/dirty/git-master_linux/camera/core_scf/src/services/gl/EGLStreamProducer.cpp:399) (in Mutex.cpp, function lock(), line 79)
Jul 16 18:24:04 user-desktop systemd[1]: nvargus-daemon.service: Main process exited, code=killed, status=11/SEGV
Jul 16 18:24:04 user-desktop systemd[1]: nvargus-daemon.service: Failed with result 'signal'.
Jul 16 18:24:04 user-desktop CRON[8790]: (CRON) info (No MTA installed, discarding output)
Jul 16 18:24:04 user-desktop systemd[1]: nvargus-daemon.service: Service hold-off time over, scheduling restart.
Jul 16 18:24:04 user-desktop systemd[1]: nvargus-daemon.service: Scheduled restart job, restart counter is at 2.
Jul 16 18:24:04 user-desktop systemd[1]: Stopped Argus daemon.
Jul 16 18:24:04 user-desktop systemd[1]: Started Argus daemon.
Jul 16 18:24:23 user-desktop bamfdaemon[7417]: bamf_matcher_set_starting_desktop_file: assertion 'desktop_file != NULL' failed
Jul 16 18:24:23 user-desktop bamfdaemon[7417]: bamf_matcher_set_starting_desktop_file: assertion 'desktop_file != NULL' failed

I would realy appreciate your help!!

What about save to disk then upload the image to cloud?

What do you mean by ‘same to’?
I’ve test my code line by line, and it seems to failed in this line when I put the command in crontab.

window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)

And if I run the whole code in terminal, it just worked perfectly.

Sorry typo I means save the image to disk first.