API Script for iPad Restart

dtmille2
Contributor III

Hello,

I have an iPad that is used as a permanent Time Clock in single app mode. I am hoping to find a way to make it restart automatically on a weekly basis.

Does anyone know what the JAMF API script would be to do so?

Thanks!

11 REPLIES 11

ryan_ball
Valued Contributor

@dtmille2 This should work:

#!/bin/bash

# https://www.jamf.com/jamf-nation/discussions/30751/api-script-for-ipad-restart

jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url | sed s'/.$//')

apiUser="user_here"
apiPass='password_here'
deviceID="device_ID_here"

data="<mobile_device_command><general><command>RestartDevice</command></general><mobile_devices><mobile_device><id>${deviceID}</id></mobile_device></mobile_devices></mobile_device_command>"

echo "Attempting to send a RestartDevice command to Mobile Device with ID: $deviceID"
curl -ksu "$apiUser:$apiPass" -H "Content-Type: text/xml" "$jssURL/JSSResource/mobiledevicecommands/command" -d $data -X POST

exit 0

dtmille2
Contributor III

Thank you! I will give this a go. Appreciate you taking the time.

tak10
Contributor II

Do you know how to run this against a smart or static device group? I need to restart multiple devices but other than iterating against list of device ID, not sure if this API can be ran against device groups.

koalatee
Contributor II

It does not appear that you can run it against a group. You can send to multiple device IDs at the same time though. 336c5b18e8344bca9aef5e0161aae3f1

tak10
Contributor II

I got it working with multiple devices. If you need to run daily or weekly reboot against specific set of devices., first create an advanced mobile device search with a criteria to group the restart devices. You will need to find the advanced mobile device search ID, you can find the advanced mobile device search ID by clicking the advanced mobile device search you just created and the URL should show up like this.

https://jss.domain.com:8443/advancedMobileDeviceSearches.html?id=1&o=r

ID will be in the URL. Use this for amds_id variable. Other variables should be easy to find. The first curl command with bunch of sed command is not pretty but it works for me for now. If you can think of better way of doing it, let me know.

#!/bin/sh
# Define Variables

### JAMF Pro username / password
apiUser="user_name"
apiPass='password'

### JAMF Pro URL
jssurl="jss.domain.com:8443"

### advanced mobile device search ID number
amds_id=""

# Get Mobile Device IDs from advanced mobile device search. This will retreive all device IDs from advanced mobile device search id 1. Change the id # to retreive from different advanced mobile device search.
mobile_devices=`curl -ksu "$apiUser:$apiPass" -X GET "https://$jssurl/JSSResource/advancedmobiledevicesearches/id/$amds_id" -H "accept: application/xml" 
| xpath /advanced_mobile_device_search/mobile_devices/mobile_device/id | sed 's/<id>//g' | sed 's/</id>/,/g' | rev | sed 's/,//' | rev`

# Send restart command against the mobile device IDs retreive in the previous command. 
curl -ksu "$apiUser:$apiPass" -X POST "https://$jssurl/JSSResource/mobiledevicecommands/command/RestartDevice/id/$mobile_devices" -H "accept: application/xml"

exit

Bkilleen
New Contributor

Okay, so I am fairly new to this, I am looking at your code, where in there do I set the schedule for the devices to restart, can I set it to restart from say 12:00AM to 01:00AM? I don't want them to run into an issue and I would rather them all restart on their own between this time. I also cannot find where I put this code in. I went to the API URL but i don't see where I can enter the code into.

You have to run the script as part of a cron job (launchd) on a mac. That is how you specify when and how often to run it. This is just the script itself. There is no way to accomplish this by jamf, unfortunately. 

maxwolf
New Contributor

Trying to use this, it seems the line 

mobile_devices=`curl <code> | rev`

is giving a syntax error, any idea?

indeskize
New Contributor II

I am getting a similar error. were you able to get this working?

mattriley
New Contributor II

New to jamf. I've seen a few threads like this about scheduling iPad restarts and that's exactly what I want to do.

What does the script above need to run on? Is there jamf server software I need to have in order to make scheduled iPad restarts work? I currently only have a jamf NOW account and have my iPad successfully enrolled in it.

Thanks for any pointers you can offer! :-)
Matt

You need to setup a cron job (launchd) to run the script on schedule on any macOS device thats always on.